ClientCredentials Class

C# | VB.NET | C++
Represents the credentials used to authenticate a client.

Inheritance

Object
    ClientCredentials

Syntax

[SerializableAttribute]
public class ClientCredentials

Remarks

The ClientCredentials class provides a way to store and manage the credentials that a client application uses to authenticate itself to a server. This can include user names, passwords, certificates, or other authentication tokens.

This class is often used in conjunction with network communication protocols that require authentication, such as HTTP, FTP, or custom protocols.

The ClientCredentials class is SerializableAttribute, meaning instances of this class can be serialized and deserialized, which is useful for saving and restoring credential information.

Constructors

Name Description
ClientCredentials() Initializes a new instance of the ClientCredentials class.

Properties

Name Description
ClientCertificate Gets or sets the X.509 certificate used for client authentication.
Password Gets or sets the password used for client authentication.
UnsecurePassword Gets or sets the password as a character array. This property is deprecated.
UserName Gets or sets the user name used for client authentication.

Constructors Details

ClientCredentials()

public ClientCredentials()

Initializes a new instance of the ClientCredentials class with default values.

Properties Details

ClientCertificate

public X509Certificate2? ClientCertificate { get; set; }

Gets or sets the X509Certificate2 object that represents the client certificate used for authentication. This is useful for scenarios requiring certificate-based authentication.

Password

public string? Password { get; set; }

Gets or sets the password as a string. This is commonly used for username/password authentication schemes.

UnsecurePassword

[ObsoleteAttribute("The Password property should be used instead of UnsecurePassword. The UnsecurePassword property is deprecated because it is not secure.")]
public char[]? UnsecurePassword { get; set; }

Gets or sets the password as a character array. This property is deprecated due to security concerns, as character arrays can be more vulnerable to memory inspection than strings. Use the Password property instead.

UserName

public string? UserName { get; set; }

Gets or sets the user name for client authentication. This is often used in conjunction with the Password property.

Example

The following example shows how to create a ClientCredentials object and set its properties to authenticate a client using username and password.

// Using System.Net;

try
{
    // Create a new ClientCredentials object
    ClientCredentials credentials = new ClientCredentials();

    // Set the username and password
    credentials.UserName = "myuser";
    credentials.Password = "mypassword123";

    // Now you can use these credentials with a network client, for example:
    // WebClient webClient = new WebClient();
    // webClient.Credentials = credentials;
    // string result = webClient.DownloadString("http://example.com/secure_resource");
    // Console.WriteLine(result);

    Console.WriteLine("Client credentials set successfully.");
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}

Requirements

Namespace
System.Net.Security
Assembly
System.Net.dll

See Also

NetworkCredential Class
X509Certificate2 Class
WebRequest Class
HttpClient Class