CredentialCache Class
Assembly: System (in System.dll)
Overview
Stores collections of network credentials, such as user names and passwords, that can be used for authentication. This class provides a cache for network credentials, allowing applications to manage and retrieve credentials for various network resources.
public class CredentialCache : System.Collections.ICollection, System.Collections.IEnumerable
The CredentialCache
class is fundamental for managing authentication in .NET networking applications. It enables you to associate specific credentials with network endpoints (like URIs or hostnames) and their corresponding authentication schemes (e.g., Basic, Digest, NTLM).
Constructors
-
CredentialCache()
public CredentialCache()
Initializes a new instance of the
CredentialCache
class.
Properties
-
Count
public int Count { get; }
Gets the number of credential collections in the cache.
-
IsPersisted
public bool IsPersisted { get; }
Indicates whether the cache is persisted.
Methods
-
Add
public void Add(Uri uri, System.Net.NetworkCredential credential)
Adds a
NetworkCredential
to the cache for the specified URI. -
Add
public void Add(string host, int port, string authenticationType, System.Net.NetworkCredential credential)
Adds a
NetworkCredential
to the cache for the specified host, port, and authentication type. -
GetCredentials
public System.Net.ICredentials GetCredentials(Uri uri, string authenticationType)
Retrieves the credentials associated with the specified URI and authentication type.
-
Remove
public void Remove(Uri uri)
Removes all credentials associated with the specified URI from the cache.
Remarks
The CredentialCache
class is used to store and manage credentials for network authentication. When a network operation requires authentication, the system can query the CredentialCache
to find appropriate credentials for the target resource.
You can add credentials to the cache programmatically using the Add
method, specifying the URI or host/port details and the associated NetworkCredential
. The cache can also be used to specify the default credentials for an application by assigning an instance of CredentialCache
to the static HttpClient.DefaultCredentials
property or the CredentialCache.DefaultCredentials
property.
When using the Add(string host, int port, string authenticationType, NetworkCredential credential)
overload, the authenticationType
parameter specifies the security protocol for which the credential is intended (e.g., "Basic", "Digest", "NTLM").
Example
using System;
using System.Net;
public class Example
{
public static void Main(string[] args)
{
// Create a new CredentialCache
CredentialCache cache = new CredentialCache();
// Define credentials for a specific URI
Uri targetUri = new Uri("http://www.example.com/resource");
NetworkCredential userCredential = new NetworkCredential("myusername", "mypassword");
// Add the credentials to the cache
cache.Add(targetUri, userCredential);
// Retrieve credentials for the URI
ICredentials retrievedCredentials = cache.GetCredentials(targetUri, "Basic");
if (retrievedCredentials != null)
{
Console.WriteLine($"Credentials found for {targetUri}:");
Console.WriteLine($"Username: {retrievedCredentials.GetCredential(targetUri, "Basic").UserName}");
}
else
{
Console.WriteLine($"No credentials found for {targetUri}.");
}
// Add credentials for a host and port
NetworkCredential adminCredential = new NetworkCredential("admin", "securepass");
cache.Add("ftp.example.com", 21, "Basic", adminCredential);
Console.WriteLine($"\nTotal items in cache: {cache.Count}");
}
}