2 out of 4 rated this helpful - Rate this topic

NetworkCredential Class

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

System.Object
  System.Net.NetworkCredential

Namespace:  System.Net
Assembly:  System (in System.dll)
public class NetworkCredential : ICredentials, 
	ICredentialsByHost

The NetworkCredential type exposes the following members.

  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library NetworkCredential() Initializes a new instance of the NetworkCredential class.
Public method NetworkCredential(String, SecureString) Initializes a new instance of the NetworkCredential class with the specified user name and password.
Public method Supported by the XNA Framework Supported by Portable Class Library NetworkCredential(String, String) Initializes a new instance of the NetworkCredential class with the specified user name and password.
Public method NetworkCredential(String, SecureString, String) Initializes a new instance of the NetworkCredential class with the specified user name, password, and domain.
Public method Supported by the XNA Framework Supported by Portable Class Library NetworkCredential(String, String, String) Initializes a new instance of the NetworkCredential class with the specified user name, password, and domain.
Top
  Name Description
Public property Supported by the XNA Framework Supported by Portable Class Library Domain Gets or sets the domain or computer name that verifies the credentials.
Public property Supported by the XNA Framework Supported by Portable Class Library Password Gets or sets the password for the user name associated with the credentials.
Public property SecurePassword Gets or sets the password as a SecureString instance.
Public property Supported by the XNA Framework Supported by Portable Class Library UserName Gets or sets the user name associated with the credentials.
Top
  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetCredential(Uri, String) Returns an instance of the NetworkCredential class for the specified Uniform Resource Identifier (URI) and authentication type.
Public method GetCredential(String, Int32, String) Returns an instance of the NetworkCredential class for the specified host, port, and authentication type.
Public method Supported by the XNA Framework Supported by Portable Class Library GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library ToString Returns a string that represents the current object. (Inherited from Object.)
Top

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.

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

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 WebRequest object, which uses it to authenticate requests to an Internet server.


NetworkCredential myCred = new NetworkCredential(
	SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain);

CredentialCache myCache = new CredentialCache();

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

WebRequest wr = WebRequest.Create("www.contoso.com");
wr.Credentials = myCache;



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Sample code throws exception
When calling:
myCache.Add(new Uri("www.contoso.com"), "Basic", myCred); 

...an exception is thrown with this message:

"Invalid URI: The format of the URI could not be determined."