System.Net.Security.NegotiateCredentials Class

Represents the credentials that can be used to authenticate a client or server.

Overview

The NegotiateCredentials class provides a way to manage the credentials used for network authentication with the Negotiate security protocol. This protocol allows for mutual authentication between clients and servers, often leveraging Kerberos or NTLM. Instances of this class are typically used when establishing secure connections, such as SSL/TLS or when performing Windows authentication.

It is part of the System.Net.Security namespace.

Syntax


public sealed class NegotiateCredentials : ICredentials
            

Remarks

The sealed keyword indicates that this class cannot be inherited.

Implementing the ICredentials interface means that this class must provide an implementation for the GetCredential method, which is used by network protocols to obtain the appropriate credentials.

Constructors

NegotiateCredentials()

Initializes a new instance of the NegotiateCredentials class with default values. This constructor typically uses the current user's credentials.


public NegotiateCredentials();
            

NegotiateCredentials(string userName)

Initializes a new instance of the NegotiateCredentials class with the specified user name. The password will be prompted for or retrieved from the current context.


public NegotiateCredentials(string userName);
            

NegotiateCredentials(string userName, string password)

Initializes a new instance of the NegotiateCredentials class with the specified user name and password.


public NegotiateCredentials(string userName, string password);
            

NegotiateCredentials(string userName, string password, string domain)

Initializes a new instance of the NegotiateCredentials class with the specified user name, password, and domain.


public NegotiateCredentials(string userName, string password, string domain);
            

Methods

GetCredential(Uri service)

Returns a CredentialCache object containing the credentials for the specified URI.


public CredentialCache GetCredential(Uri service);
            

Parameters

Name Type Description
service System.Uri The URI to obtain credentials for.

Return Value

A CredentialCache object containing the credentials.

Properties

This class does not expose any public properties directly. Its functionality is primarily accessed through its constructors and the GetCredential method inherited from ICredentials.

Example Usage

The following example demonstrates how to use NegotiateCredentials to authenticate with a web server.


using System;
using System.Net;
using System.Net.Security;

public class NegotiateExample
{
    public static void Main(string[] args)
    {
        try
        {
            // Using current user's credentials
            NegotiateCredentials credentials = new NegotiateCredentials();
            
            // Or specifying credentials explicitly
            // NegotiateCredentials credentials = new NegotiateCredentials("myuser", "mypassword", "mydomain");

            Uri targetUri = new Uri("https://secure.example.com/");

            // Create a WebRequest with the specified credentials
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUri);
            request.Credentials = credentials; // Assign the NegotiateCredentials
            request.Method = "GET";

            // Get the response (this will trigger the authentication)
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                Console.WriteLine($"Status Code: {response.StatusCode}");
                // Process the response here
            }
        }
        catch (WebException ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
            if (ex.Response != null)
            {
                Console.WriteLine($"Error Status Code: {(int)((HttpWebResponse)ex.Response).StatusCode}");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An unexpected error occurred: {ex.Message}");
        }
    }
}
            

See Also