NetworkCredentialCache Class
Namespace: System.Net.Security
Assembly: System.Net.Primitives (in System.Net.Primitives.dll)
Summary
Stores the user's credentials for network resources. This class is used by classes such as WebRequest and WebClient to provide credentials to authenticate the user to a network resource.
The NetworkCredentialCache class is part of the .NET Framework's networking capabilities. It provides a mechanism for managing and providing user credentials to various network protocols like HTTP, FTP, and others. This class plays a crucial role in scenarios where authentication is required to access network resources.
Inheritance Hierarchy
System.ObjectSystem.Net.ICredentialPolicySystem.Net.Security.NetworkCredentialCache
Syntax
public sealed class NetworkCredentialCache : ICredentialPolicy
Constructors
NetworkCredentialCache()
Initializes a new instance of the NetworkCredentialCache class.
public NetworkCredentialCache();
Methods
Add(string host, string authenticationType, System.Net.NetworkCredential credential)
Adds a credential to the cache for a specific host and authentication type.
public void Add(string host, string authenticationType, NetworkCredential credential);
Parameters
| Name | Type | Description |
|---|---|---|
host |
string |
The host to associate the credential with. |
authenticationType |
string |
The type of authentication (e.g., "Basic", "Digest"). |
credential |
System.Net.NetworkCredential |
The network credential to store. |
Remove(string host, string authenticationType)
Removes a credential from the cache.
public bool Remove(string host, string authenticationType);
Parameters
| Name | Type | Description |
|---|---|---|
host |
string |
The host to remove the credential from. |
authenticationType |
string |
The type of authentication to remove. |
Returns
| Type | Description |
|---|---|
bool |
true if the credential was successfully removed; otherwise, false. |
Properties
DefaultNetworkCredential
Gets or sets the default network credential for the current user.
public static System.Net.NetworkCredential DefaultNetworkCredential { get; set; }
Remarks
The NetworkCredentialCache class is an implementation of the ICredentialPolicy interface. It allows you to programmatically manage the credentials used for network authentication. You can add specific credentials for different hosts and authentication schemes, and the system will use these credentials when making network requests.
This class is particularly useful in applications that need to interact with authenticated network resources without requiring the user to repeatedly enter their credentials. It supports various authentication methods, including Basic, Digest, NTLM, and Kerberos.
Example
Using NetworkCredentialCache to store credentials
The following example demonstrates how to create a NetworkCredentialCache, add a credential for a specific host and authentication type, and then use it with a WebRequest.
using System;
using System.Net;
using System.Net.Security;
public class CredentialCacheExample
{
public static void Main(string[] args)
{
// Create a new NetworkCredentialCache
NetworkCredentialCache cache = new NetworkCredentialCache();
// Define the network resource details
string host = "example.com";
string authType = "Basic";
NetworkCredential credentials = new NetworkCredential("username", "password123");
// Add the credential to the cache
cache.Add(host, authType, credentials);
Console.WriteLine($"Credential added for host: {host}, type: {authType}");
// Create a WebRequest
try
{
WebRequest request = WebRequest.Create($"http://{host}/resource");
request.Credentials = cache; // Assign the credential cache to the request
// You would typically send the request here and handle the response
// For demonstration, we'll just show that the credentials are set
Console.WriteLine("WebRequest configured with NetworkCredentialCache.");
// Example: HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Console.WriteLine($"Response Status: {response.StatusCode}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
Requirements
| Tool | Version |
|---|---|
| .NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.6, 4.7, 4.8 |
| .NET Core | 2.0, 2.1, 2.2, 3.0, 3.1 |
| .NET Standard | 1.3, 2.0, 2.1 |
| Visual Studio | 2003, 2005, 2008, 2010, 2012, 2013, 2015, 2017, 2019, 2022 |