System.Net.CredentialCache Class

Assembly: System.dll
Namespace: System.Net
Summary
Provides storage for multiple credentials.
C# Syntax:
public class CredentialCache : ICredentials, IEnumerable
Remarks
The CredentialCache class stores credentials for multiple Internet resources. Applications that need to access multiple resources can store the credentials for those resources in a CredentialCache instance that then provides the proper set of credentials to the Internet resource when required. When the CredentialCache.GetCredential method is called, it compares the URI and authentication type provided with those stored in the cache and returns the first set of credentials that match.

The CredentialCache.DefaultCredentials property contains the system credentials of the current security context. For client applications, these represent the user name, password, and domain of the user who is currently logged in. For ASP.NET applications, the default credentials are the user credentials of the logged-in user or the user being impersonated.

Example
The following example initializes a CredentialCache with multiple security credentials and uses those credentials with a WebRequest:
    CredentialCache myCache = new CredentialCache();

    myCache.Add(new Uri("http://www.contoso.com/"),"Basic",new NetworkCredential("myUser","myPass"));
    myCache.Add(new Uri("http://www.contoso.com/"),"Digest", new NetworkCredential("myUser","myPass","myDomain"));

    wReq.Credentials = myCache;

    
See also:
System.Net Namespace | ICredentials | WebRequest.Credentials

System.Net.CredentialCache Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Creates a new instance of the CredentialCache class.
Public Properties
DefaultCredentials Read-only

Gets the system credentials of the application.
Public Methods
Add Adds a NetworkCredential instance to the credential cache.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetCredential Returns the NetworkCredential instance associated with the specified URI and authentication type.
GetEnumerator Returns an enumerator that can iterate through the CredentialCache instance.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
Remove Deletes a NetworkCredential instance from the cache.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Net.CredentialCache Member Details

ctor #1
Summary
Creates a new instance of the CredentialCache class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public CredentialCache();
Remarks
The constructor creates a CredentialCache instance with its CredentialCache.DefaultCredentials property initialized to the system credentials of the current security context. For client applications, these represent the user name, password, and domain of the user who is currently logged in. For ASP.NET applications, the credential is the process token of the IIS server or the process token being impersonated by the IIS server.

Use the CredentialCache.Add method to add additional credentials to the CredentialCache instance.

Example
The following example initializes a CredentialCache with multiple security credentials and uses those credentials with a WebRequest:
    CredentialCache myCache = new CredentialCache();

    myCache.Add(new Uri("http://www.contoso.com/"),"Basic",new NetworkCredential("myUser","myPass"));
    myCache.Add(new Uri("http://www.contoso.com/"),"Digest", new NetworkCredential("myUser","myPass","myDomain"));

    wReq.Credentials = myCache;

    

Return to top


Property: DefaultCredentials (read-only)
Summary
Gets the system credentials of the application.
C# Syntax:
public static ICredentials DefaultCredentials {get;}
Remarks
The CredentialCache.DefaultCredentials property applies only to NTLM, negotiate, and Kerberos-based authentication.

CredentialCache.DefaultCredentials represents the system credentials for the current security context in which the application is running. For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application. For ASP.NET applications, the default credentials are the user credentials of the logged-in user, or the user being impersonated.



Note The ICredentials instance returned by CredentialCache.DefaultCredentials cannot be used to view the user name, password, or domain of the current security context.
.NET Framework Security:
EnvironmentPermission for reading the user's system credentials. Associated enumeration: EnvironmentPermissionAccess

Return to top


Method: Add(
   Uri uriPrefix,
   string authType,
   NetworkCredential cred
)
Summary
Adds a NetworkCredential instance to the credential cache.
C# Syntax:
public void Add(
   Uri uriPrefix,
   string authType,
   NetworkCredential cred
);
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 null

-or-

authType is null.

ArgumentException The same credentials are added more than once.
Remarks
The CredentialCache.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 CredentialCache.GetCredential method is called, it returns the proper matching NetworkCredential instance.
Example
The following example initializes a CredentialCache with multiple security credentials and uses those credentials with a WebRequest:
    CredentialCache myCache = new CredentialCache();

    myCache.Add(new Uri("http://www.contoso.com/"),"Basic",new NetworkCredential("myUser","myPass"));
    myCache.Add(new Uri("http://www.contoso.com/"),"Digest", new NetworkCredential("myUser","myPass","myDomain"));

    wReq.Credentials = myCache;

    

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~CredentialCache();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetCredential(
   Uri uriPrefix,
   string authType
)
Summary
Returns the NetworkCredential instance associated with the specified URI and authentication type.
C# Syntax:
public NetworkCredential GetCredential(
   Uri uriPrefix,
   string authType
);
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.

Return Value:
A NetworkCredential or, if there is no matching credential in the cache, null.
Implements:
ICredentials.GetCredential
Remarks
The CredentialCache.GetCredential method searches the CredentialCache and returns the NetworkCredential instance for the specified URI and authorization type. If the CredentialCache contains no matching NetworkCredential instance, null is returned.

CredentialCache.GetCredential uses the longest matching URI prefix in the cache to determine which set of credentials to return for an authorization type. The following table shows examples.



URI Prefix Matches
http://www.contoso.com/portal/news.htm Requests for the specific Web page news.htm .
http://www.contoso.com/portal/ Requests for all content in the portal path, except the page news.htm .
http://www.contoso.com/ Requests for all resources at www.contoso.com , except those in the portal path.

Return to top


Method: GetEnumerator()
Summary
Returns an enumerator that can iterate through the CredentialCache instance.
C# Syntax:
public IEnumerator GetEnumerator();
Return Value:
An IEnumerator for the CredentialCache.
Implements:
IEnumerable.GetEnumerator

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Remove(
   Uri uriPrefix,
   string authType
)
Summary
Deletes a NetworkCredential instance from the cache.
C# Syntax:
public void Remove(
   Uri uriPrefix,
   string authType
);
Parameters:

uriPrefix

A Uri that specifies the URI prefix of the resources that the credential is used for.

authType

The authentication scheme used by the host named in uriPrefix.

Remarks
The CredentialCache.Remove method removes the specified NetworkCredential instance from the CredentialCache. Multiple calls to the CredentialCache.Remove method for the same NetworkCredential have no effect.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.