Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
SetCertificate Method (String, StoreLocation, StoreName)

X509CertificateInitiatorClientCredential.SetCertificate Method (String, StoreLocation, StoreName)

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

Namespace:  System.ServiceModel.Security
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

'Declaration
Public Sub SetCertificate ( _
	subjectName As String, _
	storeLocation As StoreLocation, _
	storeName As StoreName _
)

Parameters

subjectName
Type: System.String

Subject distinguished name.

storeLocation
Type: System.Security.Cryptography.X509Certificates.StoreLocation

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

storeName
Type: System.Security.Cryptography.X509Certificates.StoreName

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

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.

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);

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft