共用方式為


X509CertificateInitiatorClientCredential.SetCertificate 方法

定義

指定要用來表示服務的憑證。

多載

SetCertificate(String, StoreLocation, StoreName)

允許您藉由指定主體辨別名稱,指定要用來表示服務的憑證。

SetCertificate(StoreLocation, StoreName, X509FindType, Object)

允許您藉由指定查詢參數 (例如,storeLocationstoreNamefindTypefindValue),指定用於表示用戶端的憑證。

SetCertificate(String, StoreLocation, StoreName)

來源:
X509CertificateInitiatorClientCredential.cs
來源:
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)

來源:
X509CertificateInitiatorClientCredential.cs
來源:
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 列舉型別中。

最常用的列舉型別 (Enumeration) 是 FindBySubjectName,它會針對指定之存放區內憑證的主體名稱進行不區分大小寫的搜尋。 這可以是不精確的搜尋。 如果傳回一個以上的憑證,則會使用第一個符合搜尋的憑證來表示用戶端。

適用於