This documentation is archived and is not being maintained.

CredentialCache.Add Method

Adds a NetworkCredential instance to the credential cache.

[Visual Basic]
Public Sub Add( _
   ByVal uriPrefix As Uri, _
   ByVal authType As String, _
   ByVal cred As NetworkCredential _
public void Add(
 Uri uriPrefix,
 string authType,
 NetworkCredential cred
public: void Add(
 Uri* uriPrefix,
 String* authType,
 NetworkCredential* cred
public function Add(
   uriPrefix : Uri,
 authType : String,
 cred : NetworkCredential


A Uri that specifies the URI prefix of the resources that the credential grants access to.
The authentication scheme used by the resource named in uriPrefix.
The NetworkCredential to add to the credential cache.


Exception Type Condition
ArgumentNullException uriPrefix is a null reference (Nothing in Visual Basic)


authType is a null reference (Nothing).

ArgumentException The same credentials are added more than once.


The Add method places a NetworkCredential instance into the CredentialCache. The cache stores credentials in the order in which they are added to it. When the GetCredential method is called, it returns the proper matching NetworkCredential instance.


[Visual Basic, C#, C++] The following example initializes a CredentialCache with multiple security credentials and uses those credentials with a WebRequest:

[Visual Basic] 
Dim myCache As New CredentialCache()

myCache.Add(New Uri(""), "Basic", New NetworkCredential(UserName, SecurelyStoredPassword))
myCache.Add(New Uri(""), "Digest", New NetworkCredential(UserName, SecurelyStoredPassword, Domain))

wReq.Credentials = myCache

CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri(""),"Basic",new NetworkCredential(UserName,SecurelyStoredPassword));
myCache.Add(new Uri(""),"Digest", new NetworkCredential(UserName,SecurelyStoredPassword,Domain));

wReq.Credentials = myCache;

CredentialCache* myCache = new CredentialCache();

myCache->Add(new Uri(S""),S"Basic",new NetworkCredential(UserName,SecurelyStoredPassword));
myCache->Add(new Uri(S""),S"Digest", new NetworkCredential(UserName,SecurelyStoredPassword,Domain));

wReq->Credentials = myCache;

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

CredentialCache Class | CredentialCache Members | System.Net Namespace