本文讨论在使用传输安全性时使用 X.509 证书进行服务器和客户端身份验证。 有关 X.509 证书的详细信息,请参阅 X.509 公钥证书。 证书必须由证书颁发机构颁发,该颁发机构通常是证书的第三方颁发者。 在 Windows Server 域中,Active Directory 证书服务可用于向域上的客户端计算机颁发证书。 在此方案中,该服务托管在 Internet Information Services (IIS)下,该服务配置有安全套接字层 (SSL)。 该服务使用 SSL(X.509)证书进行配置,以允许客户端验证服务器的标识。 客户端还配置了 X.509 证书,该证书允许服务验证客户端的身份。 服务器的证书必须由客户端信任,并且客户端的证书必须由服务器信任。 服务和客户端如何验证彼此标识的实际机制超出了本文的范围。 有关详细信息,请参阅维基百科上的 数字签名 。
此方案实现请求/回复消息模式,如下图所示。
使用证书
有关将证书用于服务的详细信息,请参阅“使用证书”和“如何:使用 SSL 证书配置端口”。 下表描述了方案的各种特征。
特征 | DESCRIPTION |
---|---|
安全模式 | 运输 |
互操作性 | 使用现有的 Web 服务客户端和服务。 |
身份验证(服务器) 身份验证(客户端) |
是(使用 SSL 证书) 是(使用 X.509 证书) |
数据完整性 | 是的 |
数据机密性 | 是的 |
运输 | HTTPS |
捆绑 | WSHttpBinding |
配置服务
由于此方案中的服务托管在 IIS 下,因此使用 web.config 文件对其进行配置。 以下 web.config 展示了如何配置 WSHttpBinding 以使用传输安全和 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>
配置客户端
可以在代码或 app.config 文件中配置客户端。 以下示例演示如何在代码中配置客户端。
// 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();
或者,可以在 App.config 文件中配置客户端,如以下示例所示:
<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>