Redigera

Dela via


X509CertificateRecipientServiceCredential.SetCertificate Method

Definition

Specifies the certificate to use for representing the service.

Overloads

SetCertificate(String)

Specifies the certificate to use for representing the service by specifying the subject distinguished name.

SetCertificate(String, StoreLocation, StoreName)

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

SetCertificate(StoreLocation, StoreName, X509FindType, Object)

Specifies the certificate to use for representing the service by specifying query parameters such as storeLocation, storeName, findType and findValue.

SetCertificate(String)

Specifies the certificate to use for representing the service by specifying the subject distinguished name.

public:
 void SetCertificate(System::String ^ subjectName);
public void SetCertificate (string subjectName);
member this.SetCertificate : string -> unit
Public Sub SetCertificate (subjectName As String)

Parameters

subjectName
String

Subject distinguished name.

Examples

The following code shows how to use this method to set a certificate for a service credential from a subject name.

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");
sh.Open();
Dim b As New NetTcpBinding()
b.Security.Mode = SecurityMode.Message
Dim c As Type = GetType(ICalculator)
Dim a As New Uri("net.tcp://MyMachineName/tcpBase")
Dim baseAddresses() As Uri = { a }
Dim sh As New ServiceHost(GetType(MyService), baseAddresses)
sh.AddServiceEndpoint(c, b, "Aloha")
sh.Credentials.ServiceCertificate.SetCertificate("CN=Administrator,CN=Users,DC=johndoe,DC=nttest,DC=microsoft,DC=com")
sh.Open()

Remarks

For more information on the subjectName parameter, see SubjectName.

Applies to

SetCertificate(String, StoreLocation, StoreName)

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

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 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();
Dim b As New NetTcpBinding()
b.Security.Mode = SecurityMode.Message
Dim c As Type = GetType(ICalculator)
Dim a As New Uri("net.tcp://MyMachineName/tcpBase")
Dim baseAddresses() As Uri = { a }
Dim sh As New ServiceHost(GetType(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()

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)

Specifies the certificate to use for representing the service 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 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(
        StoreLocation.LocalMachine,
        StoreName.My,
        X509FindType.FindByThumbprint,
        "af1f50b20cd413ed9cd00c315bbb6dc1c08da5e6");
    sh.Open();
Dim b As New NetTcpBinding()
b.Security.Mode = SecurityMode.Message
Dim c As Type = GetType(ICalculator)
Dim a As New Uri("net.tcp://MyMachineName/tcpBase")
Dim baseAddresses() As Uri = { a }
Dim sh As New ServiceHost(GetType(MyService), baseAddresses)
sh.AddServiceEndpoint(c, b, "Aloha")
sh.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, "af1f50b20cd413ed9cd00c315bbb6dc1c08da5e6")
sh.Open()

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