HttpClientCertificateOptions Class
System.Net.Http
Summary
Represents options for client certificate handling in an HTTP client. This class allows you to configure how client certificates are selected and used for mutual authentication.
Syntax
public class HttpClientCertificateOptions
Remarks
The HttpClientCertificateOptions class is used to provide fine-grained control over client certificate selection when establishing a secure connection (TLS/SSL) with a server that requires client authentication. You can specify criteria such as the certificate's subject name, issuer name, and revocation status to ensure the correct certificate is presented.
Properties
| Name | Description |
|---|---|
X509Certificate2 ClientCertificate |
Gets or sets the specific client certificate to use for authentication. If set, this certificate will be preferred over any certificate found through other criteria. |
string StoreName |
Gets or sets the name of the certificate store to search for certificates (e.g., "My", "Root"). |
System.Security.Cryptography.X509Certificates.StoreLocation StoreLocation |
Gets or sets the location of the certificate store (e.g., LocalMachine, CurrentUser). |
string SubjectName |
Gets or sets a string that represents the distinguished name (DN) of the certificate's subject. Wildcards can be used for partial matches. |
string IssuerName |
Gets or sets a string that represents the distinguished name (DN) of the certificate's issuer. Wildcards can be used for partial matches. |
bool RevocationMode |
Gets or sets a value that indicates whether to check the certificate's revocation status. Possible values are CheckForRevocation or NoCheck. |
Constructors
public HttpClientCertificateOptions()
Initializes a new instance of the HttpClientCertificateOptions class with default settings.
Methods
public override string ToString()
Returns a string representation of the HttpClientCertificateOptions object.
Example
The following example demonstrates how to configure HttpClientCertificateOptions to select a client certificate based on its subject name and store location.
C#
VB.NET
C#
Example
using System;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
public class Example
{
public static async Task UseClientCertificateOptions()
{
var options = new HttpClientCertificateOptions
{
StoreName = "My",
StoreLocation = StoreLocation.CurrentUser,
SubjectName = "CN=MyClientApp, OU=IT, O=MyCompany",
RevocationMode = X509RevocationMode.CheckForRevocation
};
var handler = new HttpClientHandler
{
ClientCertificateOptions = options
};
using (var client = new HttpClient(handler))
{
try
{
// Replace with your target URL that requires client certificate authentication
HttpResponseMessage response = await client.GetAsync("https://your-secure-api.com/data");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
}
}
}
VB.NET
Example
Imports System
Imports System.Net.Http
Imports System.Security.Cryptography.X509Certificates
Imports System.Threading.Tasks
Public Class Example
Public Shared Async Function UseClientCertificateOptions() As Task
Dim options = New HttpClientCertificateOptions() With {
.StoreName = "My",
.StoreLocation = StoreLocation.CurrentUser,
.SubjectName = "CN=MyClientApp, OU=IT, O=MyCompany",
.RevocationMode = X509RevocationMode.CheckForRevocation
}
Dim handler = New HttpClientHandler() With {
.ClientCertificateOptions = options
}
Using client = New HttpClient(handler)
Try
' Replace with your target URL that requires client certificate authentication
Dim response As HttpResponseMessage = Await client.GetAsync("https://your-secure-api.com/data")
response.EnsureSuccessStatusCode()
Dim responseBody As String = Await response.Content.ReadAsStringAsync()
Console.WriteLine(responseBody)
Catch e As HttpRequestException
Console.WriteLine(vbCrLf & "Exception Caught!")
Console.WriteLine("Message :{0} ", e.Message)
End Try
End Using
End Function
End Class