共用方式為


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 列舉型別中。

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

適用於