Επεξεργασία

Κοινή χρήση μέσω


NetworkCredential Class

Definition

Provides credentials for password-based authentication schemes such as basic, digest, NTLM, and Kerberos authentication.

public ref class NetworkCredential : System::Net::ICredentials, System::Net::ICredentialsByHost
public ref class NetworkCredential : System::Net::ICredentials
public class NetworkCredential : System.Net.ICredentials, System.Net.ICredentialsByHost
public class NetworkCredential : System.Net.ICredentials
type NetworkCredential = class
    interface ICredentials
    interface ICredentialsByHost
type NetworkCredential = class
    interface ICredentials
Public Class NetworkCredential
Implements ICredentials, ICredentialsByHost
Public Class NetworkCredential
Implements ICredentials
Inheritance
NetworkCredential
Implements

Examples

The following code example associates a NetworkCredential object with a set of Uniform Resource Identifiers (URIs) in a CredentialCache. It then passes the CredentialCache to a HttpClient object, which uses it to authenticate requests to an Internet server.

NetworkCredential^ myCred = gcnew NetworkCredential(
   SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain );

CredentialCache^ myCache = gcnew CredentialCache;

myCache->Add( gcnew Uri( "http://www.contoso.com" ), "Basic", myCred );
myCache->Add( gcnew Uri( "http://app.contoso.com" ), "Basic", myCred );

// HttpClient lifecycle management best practices:
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
HttpClientHandler^ handler = gcnew HttpClientHandler();
handler->Credentials = myCache;
HttpClient^ client = gcnew HttpClient(handler);
NetworkCredential myCred = new NetworkCredential(
    SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain);

CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri("http://www.contoso.com"), "Basic", myCred);
myCache.Add(new Uri("http://app.contoso.com"), "Basic", myCred);

// HttpClient lifecycle management best practices:
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
HttpClient client = new HttpClient(new HttpClientHandler
{
    Credentials = myCache
});
Dim myCred As New NetworkCredential(SecurelyStoredUserName, SecurelyStoredPassword, SecurelyStoredDomain)

Dim myCache As New CredentialCache()

myCache.Add(New Uri("http://www.contoso.com"), "Basic", myCred)
myCache.Add(New Uri("http://app.contoso.com"), "Basic", myCred)
       
' HttpClient lifecycle management best practices:
' https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
Dim client As New HttpClient(New HttpClientHandler With
{
    .Credentials = myCache
})

Remarks

The NetworkCredential class is a base class that supplies credentials in password-based authentication schemes such as basic, digest, NTLM, and Kerberos. Classes that implement the ICredentials interface, such as the CredentialCache class, return NetworkCredential objects.

Note

On Unix platforms, use of NTLM authentication may require installing the NTLM plugin for the GSSAPI layer used by the implemention. On Ubuntu distributions, the package is called gss-ntlmssp. Alternatively, applications may opt in for a managed implementation of NTLM by setting the _UseManagedNtlm .csproj property (.NET 9+ only), or setting the System.Net.Security.UseManagedNtlm AppContext switch.

On Windows, NTLM and Negotiate (Kerberos) authentication always depend on GSSAPI of the operating system.

This class does not support public key-based authentication methods such as Secure Sockets Layer (SSL) client authentication.

Constructors

NetworkCredential()

Initializes a new instance of the NetworkCredential class.

NetworkCredential(String, SecureString, String)

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

NetworkCredential(String, SecureString)

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

NetworkCredential(String, String, String)

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

NetworkCredential(String, String)

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

Properties

Domain

Gets or sets the domain or computer name that verifies the credentials.

Password

Gets or sets the password for the user name associated with the credentials.

SecurePassword

Gets or sets the password as a SecureString instance.

UserName

Gets or sets the user name associated with the credentials.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetCredential(String, Int32, String)

Returns an instance of the NetworkCredential class for the specified host, port, and authentication type.

GetCredential(Uri, String)

Returns an instance of the NetworkCredential class for the specified Uniform Resource Identifier (URI) and authentication type.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to