客户端相应的将endpoint的address修改为: <endpoint address=https://wcfservicewebsite.com/HelloService.svc.../>。 运行测试站点,结果如图11-49。
图11-49 Transport模式无凭据测试结果 从图11-49的结果结合服务端代码,我们可以知道当前采用的是匿名访问。那么传输层是不是保证了传输安全呢?我们Fiddler的捕获结果,如代码清单11-102。
代码清单11-102 Transport模式下捕获的数据
CONNECT wcfservicewebsite.com:443 HTTP/1.1
Host: wcfservicewebsite.com
Proxy-Connection: Keep-Alive
The data sent represents an SSLv3-compatible ClientHello handshake. For your
convenience, the data is extracted below.
Major Version: 3
Minor Version: 1
Random: 4E 07 11 12 D7 AE D1 35 94 52 CF CE B9 EA BF 8C 5B 4E B2 D4 86 0A BC
E6 D2 61 C4 97 BC 1C 11 DA
SessionID: empty
Ciphers:
[002F] TLS_RSA_AES_128_SHA
[0035] TLS_RSA_AES_256_SHA
[0005] SSL_RSA_WITH_RC4_128_SHA
[000A] SSL_RSA_WITH_3DES_EDE_SHA
[C013] TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA
[C014] TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA
[C009] TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
[C00A] TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
[0032] TLS_DHE_DSS_WITH_AES_128_SHA
[0038] TLS_DHE_DSS_WITH_AES_256_SHA
[0013] SSL_DHE_DSS_WITH_3DES_EDE_SHA
[0004] SSL_RSA_WITH_RC4_128_MD5
代码清单11-102的内容是客户端发送的请求内容,Fiddler显示的并不是原始数据,但是足以证明数据在传输层面得到了保护,被加密传输,可以设置Fiddler对SSL进行解密,解密后的请求数据如代码清单11-103所示。
代码清单11-103 实际的请求信息
POST https://wcfservicewebsite.com/HelloService.svc HTTP/1.1
Content-Type: text/xml; charset=utf-8
VsDebuggerCausalityData:
uIDPo9B4edYyOw5ClT+7ozgCtOoAAAAAqjlegWjQxkCz0d14I8iaZDw+0oZgJs5BgmW5E+f6yIkACQAA
SOAPAction: "http://tempuri.org/IHelloService/GetHello"
Host: wcfservicewebsite.com
Content-Length: 133
Expect: 100-continue
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
<s:Envelope
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetHello
xmlns="http://tempuri.org/"/></s:Body></s:Envelope>
现在我们来配置服务端和客户端证书,分别使用上面生成的两个证书。接下来我们配置服务端证书,如代码清单11-104。
代码清单11-104 配置服务端证书
<serviceBehaviors>
<behavior
name="WcfHelloService.ServiceBehavior">
<serviceMetadata
httpGetEnabled="true"/>
<serviceDebug
includeExceptionDetailInFaults="false"/>
<serviceCredentials>
<clientCertificate>
<authentication
certificateValidationMode="PeerTrust"/>
<certificate
findValue="xClient"
storeLocation="CurrentUser"
storeName="TrustedPeople"
x509FindType="FindBySubjectName"/>
</clientCertificate>
<serviceCertificate
x509FindType="FindBySubjectName"
storeLocation="CurrentUser"
storeName="TrustedPeople"
findValue="xServer"
/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
证书配置和NetTcpBinding没有什么区别,这里我就不重复了,如果您还有疑惑可以参考前文。服务端配置好以后,我们从浏览器访问https://wcfservicewebsite.com/HelloService.svc,却出现如图11-50所示的错误。
图11-50 找不到证书错误 我在Window7上做测试偶尔会出现这样的错误,有可能是证书存储区的缓存机制造成的,不过将证书存储在LocalMachine上,可以迅速找到。这可能和IIS托管有关,笔者对此没有深入研究,如果您遇到同样的问题可以尝试将证书放在LocalMachine存储区上。 更新测试站点的配置,更新后客户的相关配置如代码清单11-105所示。
代码清单11-105 客户端证书配置
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior
name="clientBehavior">
<clientCredentials>
<clientCertificate
findValue="XuanhunClient"
storeLocation="CurrentUser"
storeName="TrustedPeople"
x509FindType="FindBySubjectName"/>
<serviceCertificate>
<authentication
certificateValidationMode="None"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
…...
<security
mode="Transport">
<transport
clientCredentialType="Certificate" proxyCredentialType="None">
</transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint
address="https://wcfservicewebsite.com/HelloService.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IHelloService"
contract="HelloServiceReferenceForBasic.IHelloService"
name="BasicHttpBinding_IHelloService" behaviorConfiguration="clientBehavior">
</endpoint>
</client>
</system.serviceModel>
代码清单11-105的配置中,我们将endpoint的address属性更改为https连接https://wcfservicewebsite.com/HelloService.svc",并配置<authentication certificateValidationMode="None"/>使得客户端不验证服务端证书。 启动测试站点,得到了如图11-51的错误。
图11-51 SSL配置不一致错误 出现图11-51的错误的原因在于服务本身要求客户端必须传递证书,而IIS的SSL设置没有对客户证书设置成必须。按图11-52修改就可以了。
图11-52 设置IIS的SSL 现在我们再次运行测试站点,结果如图11-53。
图11-53 Certificate验证成功 图11-53中成功的返回证书标识和验证类型X509,说明服务端对客户端证书的验证成功。