NetworkCredential Class
public class NetworkCredential : System.Security.SecureString
Represents a user's network credentials, such as a user name and password.
Summary
The NetworkCredential class is used to store and manage user credentials for network authentication. It allows you to specify a username, password, and domain, which are commonly required when connecting to network resources or services.
Remarks
Instances of NetworkCredential are often used with classes in the System.Net namespace, such as HttpClient, FtpWebRequest, and others that require authentication. The password is stored as a SecureString for enhanced security, preventing it from being easily accessed in plain text.
When using NetworkCredential, ensure that you handle sensitive information like passwords responsibly and securely.
Properties
| Name | Description |
|---|---|
| Domain | Gets the domain name associated with the credential. |
| Password | Gets the password associated with the credential. This property returns a SecureString. |
| UserName | Gets the user name associated with the credential. |
Constructors
| Name | Description |
|---|---|
| NetworkCredential() | Initializes a new instance of the NetworkCredential class. |
| NetworkCredential(string userName, string password) | Initializes a new instance of the NetworkCredential class with the specified user name and password. |
| NetworkCredential(string userName, SecureString password) | Initializes a new instance of the NetworkCredential class with the specified user name and password as a SecureString. |
| NetworkCredential(string userName, string password, string domain) | Initializes a new instance of the NetworkCredential class with the specified user name, password, and domain. |
| NetworkCredential(string userName, SecureString password, string domain) | Initializes a new instance of the NetworkCredential class with the specified user name, password as a SecureString, and domain. |
Methods
| Name | Description |
|---|---|
| GetCredential(Uri challengeUri, string authType) | Returns a NetworkCredential object when called by the system. |
| ToString() | Returns a string representation of the NetworkCredential object. |
Example
The following example demonstrates how to create and use a NetworkCredential object to authenticate with a network resource.
using System;
using System.Net;
using System.Security;
public class Example
{
public static void Main(string[] args)
{
// Create a SecureString for the password
SecureString securePassword = new SecureString();
securePassword.AppendChar('p');
securePassword.AppendChar('a');
securePassword.AppendChar('s');
securePassword.AppendChar('s');
securePassword.AppendChar('w');
securePassword.AppendChar('o');
securePassword.AppendChar('r');
securePassword.AppendChar('d');
securePassword.MakeReadOnly(); // Make the SecureString read-only
// Create a NetworkCredential object
NetworkCredential credentials = new NetworkCredential("myUsername", securePassword, "myDomain");
// You can now use these credentials with network requests
Console.WriteLine($"Username: {credentials.UserName}");
Console.WriteLine($"Domain: {credentials.Domain}");
// Note: Accessing the password directly is discouraged for security reasons.
// It's typically used internally by the .NET framework for authentication.
// Example of how it might be used (conceptual)
// WebClient client = new WebClient();
// client.Credentials = credentials;
// string response = client.DownloadString("http://example.com/protected-resource");
// Console.WriteLine("Resource downloaded successfully.");
}
}