X509CertificateInitiatorClientCredential.SetCertificate 方法

定义

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

重载

SetCertificate(String, StoreLocation, StoreName)

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

SetCertificate(StoreLocation, StoreName, X509FindType, Object)

使您可以通过指定查询参数(如 storeLocationstoreNamefindTypefindValue)指定用于表示客户端的证书。

SetCertificate(String, StoreLocation, StoreName)

Source:
X509CertificateInitiatorClientCredential.cs
Source:
X509CertificateInitiatorClientCredential.cs
Source:
X509CertificateInitiatorClientCredential.cs

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

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 证书存储区的名称。

示例

下面的代码指定要使用的证书。

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

注解

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

storeLocation 的值包含在 StoreLocation 枚举中:

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

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

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

storeName 的值包含在 StoreName 枚举中。

适用于

SetCertificate(StoreLocation, StoreName, X509FindType, Object)

Source:
X509CertificateInitiatorClientCredential.cs
Source:
X509CertificateInitiatorClientCredential.cs
Source:
X509CertificateInitiatorClientCredential.cs

使您可以通过指定查询参数(如 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 证书存储区中搜索的值。

示例

下面的代码指定要使用的证书。

// 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")

注解

storeLocation 的值包含在 StoreLocation 枚举中:

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

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

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

storeName 的值包含在 StoreName 枚举中。

findType 的值包含在 X509FindType 枚举中。

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

适用于