X509CertificateInitiatorClientCredential.SetCertificate Method

Definition

Specifies the certificate to use to represent the service.

Overloads

SetCertificate(String, StoreLocation, StoreName)

Allows you to specify the certificate to use to represent the service by specifying the subject distinguished name.

SetCertificate(StoreLocation, StoreName, X509FindType, Object)

Allows you to specify the certificate to use to represent the client by specifying query parameters such as storeLocation, storeName, findType and findValue.

SetCertificate(String, StoreLocation, StoreName)

Allows you to specify the certificate to use to represent the service by specifying the subject distinguished name.

public:
 void SetCertificate(System::String ^ subjectName, System::Security::Cryptography::X509Certificates::StoreLocation storeLocation, System::Security::Cryptography::X509Certificates::StoreName storeName);
public void SetCertificate (string subjectName, System.Security.Cryptography.X509Certificates.StoreLocation storeLocation, System.Security.Cryptography.X509Certificates.StoreName storeName);
member this.SetCertificate : string * System.Security.Cryptography.X509Certificates.StoreLocation * System.Security.Cryptography.X509Certificates.StoreName -> unit
Public Sub SetCertificate (subjectName As String, storeLocation As StoreLocation, storeName As StoreName)

Parameters

subjectName
String

Subject distinguished name.

storeLocation
StoreLocation

The location of the certificate store the service uses to obtain the service certificate.

storeName
StoreName

Specifies the name of the X.509 certificate store to open.

Examples

The following code specifies the certificate to use.

// Create a WSHttpBinding and set its security properties. The
// security mode is Message, and the client is authenticated with
// a certificate.
EndpointAddress ea = new EndpointAddress("http://contoso.com/");
WSHttpBinding b = new WSHttpBinding();
b.Security.Mode = SecurityMode.Message;
b.Security.Message.ClientCredentialType =
    MessageCredentialType.Certificate;

// Create the client with the binding and EndpointAddress.
CalculatorClient cc = new CalculatorClient(b, ea);

// Set the client credential value to a valid certificate.
cc.ClientCredentials.ClientCertificate.SetCertificate(
    "CN=MyName, OU=MyOrgUnit, C=US",
    StoreLocation.CurrentUser,
    StoreName.TrustedPeople);

Remarks

For more information on the subjectName parameter, see SubjectName.

Values for storeLocation are included in the StoreLocation enumeration:

  • LocalMachine: the certificate store assigned to the local machine (default).

  • CurrentUser: the certificate store used by the current user.

If the client application is running under a system account, then the certificate is typically in LocalMachine. If the client application is running under a user account, then the certificate is typically in CurrentUser.

Values for storeName are included in the StoreName enumeration.

Applies to

SetCertificate(StoreLocation, StoreName, X509FindType, Object)

Allows you to specify the certificate to use to represent the client by specifying query parameters such as storeLocation, storeName, findType and findValue.

public:
 void SetCertificate(System::Security::Cryptography::X509Certificates::StoreLocation storeLocation, System::Security::Cryptography::X509Certificates::StoreName storeName, System::Security::Cryptography::X509Certificates::X509FindType findType, System::Object ^ findValue);
public void SetCertificate (System.Security.Cryptography.X509Certificates.StoreLocation storeLocation, System.Security.Cryptography.X509Certificates.StoreName storeName, System.Security.Cryptography.X509Certificates.X509FindType findType, object findValue);
member this.SetCertificate : System.Security.Cryptography.X509Certificates.StoreLocation * System.Security.Cryptography.X509Certificates.StoreName * System.Security.Cryptography.X509Certificates.X509FindType * obj -> unit
Public Sub SetCertificate (storeLocation As StoreLocation, storeName As StoreName, findType As X509FindType, findValue As Object)

Parameters

storeLocation
StoreLocation

The location of the certificate store the client uses to obtain the client certificate.

storeName
StoreName

Specifies the name of the X.509 certificate store to open.

findType
X509FindType

Defines the type of X.509 search to be executed.

findValue
Object

The value to search for in the X.509 certificate store.

Examples

The following code specifies the certificate to use.

// Create a WSHttpBinding and set its security properties. The
// security mode is Message, and the client is authenticated with
// a certificate.
EndpointAddress ea = new EndpointAddress("http://contoso.com/");
WSHttpBinding b = new WSHttpBinding();
b.Security.Mode = SecurityMode.Message;
b.Security.Message.ClientCredentialType =
    MessageCredentialType.Certificate;

// Create the client with the binding and EndpointAddress.
CalculatorClient cc = new CalculatorClient(b, ea);

// Set the client credential value to a valid certificate.
cc.ClientCredentials.ClientCertificate.SetCertificate(
    StoreLocation.CurrentUser,
    StoreName.TrustedPeople,
    X509FindType.FindBySubjectName,
    "client.com");
' Create a WSHttpBinding and set its security properties. The
' security mode is Message, and the client is authenticated with 
' a certificate.
Dim ea As New EndpointAddress("http://contoso.com/")
Dim b As New WSHttpBinding()
b.Security.Mode = SecurityMode.Message
b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate

' Create the client with the binding and EndpointAddress.
Dim cc As New CalculatorClient(b, ea)

' Set the client credential value to a valid certificate.
cc.ClientCredentials.ClientCertificate.SetCertificate( _
   StoreLocation.CurrentUser, _
   StoreName.TrustedPeople, _
   X509FindType.FindBySubjectName, _
   "client.com")

Remarks

Values for storeLocation are included in the StoreLocation enumeration:

  • LocalMachine: the certificate store assigned to the local machine (default).

  • CurrentUser: the certificate store used by the current user.

If the client application is running under a system account, then the certificate is typically in LocalMachine. If the client application is running under a user account, then the certificate is typically in CurrentUser.

Values for storeName are included in the StoreName enumeration.

Values for findType are included in the X509FindType enumeration.

The most commonly used enumeration is FindBySubjectName, which does a case-insensitive search on the subject name of certificates in the specified store. This can be an imprecise search. If more than one certificate is returned then the first one matching the find is used to represent the client.

Applies to