X509CertificateRecipientServiceCredential.SetCertificate 方法

定义

指定用于表示服务的证书。

重载

SetCertificate(String)

通过指定主题可分辨名称指定用于表示服务的证书。

SetCertificate(String, StoreLocation, StoreName)

通过指定主题可分辨名称、证书存储区名称和存储区位置来指定用于表示服务的证书。

SetCertificate(StoreLocation, StoreName, X509FindType, Object)

通过指定查询参数(如 storeLocationstoreNamefindTypefindValue)来指定用于表示服务的证书。

SetCertificate(String)

通过指定主题可分辨名称指定用于表示服务的证书。

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

参数

subjectName
String

主题可分辨名称。

示例

下面的代码演示如何使用此方法从主题名称设置服务凭据的证书。

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

注解

有关 subjectName 参数的更多信息,请参见 SubjectName

适用于

SetCertificate(String, StoreLocation, StoreName)

通过指定主题可分辨名称、证书存储区名称和存储区位置来指定用于表示服务的证书。

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)

参数

subjectName
String

主题可分辨名称。

storeLocation
StoreLocation

服务用于获取服务证书的证书存储区位置。

storeName
StoreName

指定要打开的 X.509 证书存储区的名称。

示例

下面的代码演示如何使用此方法设置服务凭据的证书。

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

注解

有关 subjectName 参数的更多信息,请参见 SubjectName

storeLocation 的值包含在 StoreLocation 枚举中:

  • LocalMachine:分配给本地计算机的证书存储区(默认)。

  • CurrentUser:当前用户所使用的证书存储区。

如果客户端应用程序在系统帐户下运行,则证书通常位于 LocalMachine。 如果客户端应用程序在用户帐户下运行,则证书通常位于 CurrentUser

storeName 的值包含在 StoreName 枚举中。

适用于

SetCertificate(StoreLocation, StoreName, X509FindType, Object)

通过指定查询参数(如 storeLocationstoreNamefindTypefindValue)来指定用于表示服务的证书。

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)

参数

storeLocation
StoreLocation

客户端用于获取客户端证书的证书存储区位置。

storeName
StoreName

指定要打开的 X.509 证书存储区的名称。

findType
X509FindType

定义要执行的 X.509 搜索的类型。

findValue
Object

要在 X.509 证书存储区中搜索的值。

示例

下面的代码演示如何使用此方法设置服务凭据的证书。

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

注解

storeLocation 的值包含在 StoreLocation 枚举中:

  • LocalMachine:分配给本地计算机的证书存储区(默认)。

  • CurrentUser:当前用户所使用的证书存储区。

如果客户端应用程序在系统帐户下运行,则证书通常位于 LocalMachine。 如果客户端应用程序在用户帐户下运行,则证书通常位于 CurrentUser

storeName 的值包含在 StoreName 枚举中。

findType 的值包含在 X509FindType 枚举中。

最常用的枚举是 FindBySubjectName,它在指定存储区中对证书的主题名称执行不区分大小写的搜索。 这可能是不精确的搜索。 如果返回一个以上的证书,则使用与查找条件匹配的第一项表示客户端。

适用于