Export (0) Print
Expand All

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 _
)
[C#]
public void Add(
 Uri uriPrefix,
 string authType,
 NetworkCredential cred
);
[C++]
public: void Add(
 Uri* uriPrefix,
 String* authType,
 NetworkCredential* cred
);
[JScript]
public function Add(
   uriPrefix : Uri,
 authType : String,
 cred : NetworkCredential
);

Parameters

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

Exceptions

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

-or-

authType is a null reference (Nothing).

ArgumentException The same credentials are added more than once.

Remarks

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.

Example

[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("http://www.contoso.com/"), "Basic", New NetworkCredential(UserName, SecurelyStoredPassword))
myCache.Add(New Uri("http://www.contoso.com/"), "Digest", New NetworkCredential(UserName, SecurelyStoredPassword, Domain))

wReq.Credentials = myCache

[C#] 
CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri("http://www.contoso.com/"),"Basic",new NetworkCredential(UserName,SecurelyStoredPassword));
myCache.Add(new Uri("http://www.contoso.com/"),"Digest", new NetworkCredential(UserName,SecurelyStoredPassword,Domain));

wReq.Credentials = myCache;

[C++] 
CredentialCache* myCache = new CredentialCache();

myCache->Add(new Uri(S"http://www.contoso.com/"),S"Basic",new NetworkCredential(UserName,SecurelyStoredPassword));
myCache->Add(new Uri(S"http://www.contoso.com/"),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.

Requirements

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

Show:
© 2014 Microsoft