X509CertificateRecipientServiceCredential.SetCertificate Method (String, StoreLocation, StoreName)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Specifies the certificate to use for representing the service by specifying the subject distinguished name, the certificate store name and store location.

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

public void SetCertificate(
	string subjectName,
	StoreLocation storeLocation,
	StoreName 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 shows how to use this method to set a certificate for a service credential.

NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Message;
Type c = typeof(ICalculator);
Uri a = new Uri("net.tcp://MyMachineName/tcpBase");
Uri[] baseAddresses = new Uri[] { a };
ServiceHost sh = new ServiceHost(typeof(MyService), baseAddresses);
sh.AddServiceEndpoint(c, b, "Aloha");
sh.Credentials.ServiceCertificate.SetCertificate(
    "CN=Administrator,CN=Users,DC=johndoe,DC=nttest,DC=microsoft,DC=com",
    StoreLocation.LocalMachine,
    StoreName.My);
sh.Open();

.NET Framework
Available since 3.0
Return to top
Show: