System.Net Namespace


.NET Framework Class Library
NetworkCredential Class

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

Namespace:  System.Net
Assembly:  System (in System.dll)
Syntax

Visual Basic (Declaration)
Public Class NetworkCredential _
    Implements ICredentials, ICredentialsByHost
Visual Basic (Usage)
Dim instance As NetworkCredential
C#
public class NetworkCredential : ICredentials, 
    ICredentialsByHost
Visual C++
public ref class NetworkCredential : ICredentials, 
    ICredentialsByHost
JScript
public class NetworkCredential implements ICredentials, ICredentialsByHost
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.

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

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

Visual Basic
Dim myCred As New NetworkCredential(SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain )

Dim myCache As New CredentialCache()

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

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

C#
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;

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

CredentialCache^ myCache = gcnew CredentialCache;

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

WebRequest^ wr = WebRequest::Create( "www.contoso.com" );
wr->Credentials = myCache;
JScript
 var myCred : NetworkCredential = new NetworkCredential(
    SecurelyStoredUserName, SecurelyStoredPassword, SecurelyStoredDomain )

 var myCache : CredentialCache = new CredentialCache()

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

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

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

CredentialCache* myCache = new CredentialCache();

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

WebRequest* wr = WebRequest::Create(S"www.contoso.com");
wr->Credentials = myCache;

Inheritance Hierarchy

System..::.Object
  System.Net..::.NetworkCredential
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference



Page view tracker