Udostępnij za pośrednictwem


Zabezpieczanie transportu przy użyciu uwierzytelniania certyfikatów

W tym artykule omówiono używanie certyfikatów X.509 na potrzeby uwierzytelniania serwera i klienta podczas korzystania z zabezpieczeń transportu. Aby uzyskać więcej informacji na temat certyfikatów X.509, zobacz X.509 Public Key Certificates (Certyfikaty kluczy publicznych X.509). Certyfikaty muszą być wystawiane przez urząd certyfikacji, który jest często wystawcą certyfikatów innej firmy. W domenie systemu Windows Server usługi certyfikatów Active Directory mogą służyć do wystawiania certyfikatów na komputerach klienckich w domenie. W tym scenariuszu usługa jest hostowana w usługach Internet Information Services (IIS), które są skonfigurowane przy użyciu protokołu SSL (Secure Sockets Layer). Usługa jest skonfigurowana przy użyciu certyfikatu SSL (X.509), aby umożliwić klientom weryfikowanie tożsamości serwera. Klient jest również skonfigurowany przy użyciu certyfikatu X.509, który umożliwia usłudze weryfikowanie tożsamości klienta. Certyfikat serwera musi być zaufany przez klienta, a certyfikat klienta musi być zaufany przez serwer. Rzeczywista mechanika sposobu, w jaki usługa i klient weryfikuje tożsamość siebie nawzajem, wykracza poza zakres tego artykułu. Aby uzyskać więcej informacji, zobacz Podpis cyfrowy w Wikipedii.

Ten scenariusz implementuje wzorzec komunikatu żądania/odpowiedzi, jak pokazano na poniższym diagramie.

Secure transfer using certificates

Aby uzyskać więcej informacji na temat używania certyfikatu z usługą, zobacz Praca z certyfikatami i Instrukcje: konfigurowanie portu przy użyciu certyfikatu SSL. W poniższej tabeli opisano różne cechy scenariusza.

Characteristic opis
Tryb zabezpieczeń Transport
Współdziałanie Z istniejącymi klientami i usługami usługi sieci Web.
Uwierzytelnianie (serwer)

Uwierzytelnianie (klient)
Tak (przy użyciu certyfikatu SSL)

Tak (przy użyciu certyfikatu X.509)
Integralność danych Tak
Poufność danych Tak
Transport HTTPS
Wiązanie WSHttpBinding

Konfigurowanie usługi

Ponieważ usługa w tym scenariuszu jest hostowana w usługach IIS, jest skonfigurowana przy użyciu pliku web.config. W poniższej konfiguracji web.config pokazano, jak skonfigurować WSHttpBinding opcję używania zabezpieczeń transportu i poświadczeń klienta X.509.

<configuration>  
  <system.serviceModel>  
    <protocolMapping>  
      <add scheme="https" binding="wsHttpBinding" />  
    </protocolMapping>  
    <bindings>  
      <wsHttpBinding>  
        <!-- configure wsHttp binding with Transport security mode and clientCredentialType as Certificate -->  
        <binding>  
          <security mode="Transport">  
            <transport clientCredentialType="Certificate"/>
          </security>  
        </binding>  
      </wsHttpBinding>  
    </bindings>  
    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->  
    <behaviors>  
      <serviceBehaviors>  
        <behavior>
           <serviceDebug includeExceptionDetailInFaults="True" />  
        </behavior>  
      </serviceBehaviors>  
    </behaviors>  
  </system.serviceModel>  
</configuration>  

Konfigurowanie klienta

Klient można skonfigurować w kodzie lub w pliku app.config. W poniższym przykładzie pokazano, jak skonfigurować klienta w kodzie.

// Create the binding.  
var myBinding = new WSHttpBinding();  
myBinding.Security.Mode = SecurityMode.Transport;  
myBinding.Security.Transport.ClientCredentialType =  
   HttpClientCredentialType.Certificate;  
  
// Create the endpoint address. Note that the machine name
// must match the subject or DNS field of the X.509 certificate  
// used to authenticate the service.
var ea = new  
   EndpointAddress("https://localhost/CalculatorService/service.svc");  
  
// Create the client. The code for the calculator
// client is not shown here. See the sample applications  
// for examples of the calculator code.  
var cc =  
   new CalculatorClient(myBinding, ea);  
  
// The client must specify a certificate trusted by the server.  
cc.ClientCredentials.ClientCertificate.SetCertificate(  
    StoreLocation.CurrentUser,  
    StoreName.My,  
    X509FindType.FindBySubjectName,  
    "contoso.com");  
  
// Begin using the client.  
Console.WriteLine(cc.Add(100, 1111));  
//...  
cc.Close();  

Alternatywnie możesz skonfigurować klienta w pliku App.config, jak pokazano w poniższym przykładzie:

<configuration>  
  <system.serviceModel>  
    <client>  
      <!-- this endpoint has an https: address -->  
      <endpoint address=" https://localhost/CalculatorService/service.svc "
                behaviorConfiguration="endpointCredentialBehavior"  
                binding="wsHttpBinding"
                bindingConfiguration="Binding1"
                contract="Microsoft.Samples.TransportSecurity.ICalculator"/>  
    </client>  
    <behaviors>  
      <endpointBehaviors>  
        <behavior name="endpointCredentialBehavior">  
          <clientCredentials>  
            <clientCertificate findValue="contoso.com"  
                               storeLocation="CurrentUser"  
                               storeName="My"  
                               x509FindType="FindBySubjectName" />  
          </clientCredentials>  
        </behavior>  
      </endpointBehaviors>  
    </behaviors>  
    <bindings>  
      <wsHttpBinding>  
        <!-- configure wsHttpbinding with Transport security mode  
                   and clientCredentialType as Certificate -->  
        <binding name="Binding1">  
          <security mode="Transport">  
            <transport clientCredentialType="Certificate"/>  
          </security>  
        </binding>  
      </wsHttpBinding>  
    </bindings>  
  </system.serviceModel>  
  
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>  

Zobacz też