代码改变世界

WCF 第八章 安全 服务身份

2011-01-19 09:59  DanielWise  阅读(806)  评论(3编辑  收藏  举报

当在客户端和服务端之间创建一个安全通信信道时,客户端可以通过本章描述的很多方法来与服务端认证。客户端可以使用用户名/密码认证,Windows认证或者证书认证。然而同等重要的是认证服务端。如果一个客户端将要与服务端交换敏感信息,那么服务端认证就和客户端认证一样重要。这么做如果失败就会在服务端发生互联网上流行的欺骗诈骗。为了保护以防止这种事情发生,WCF在通过传输层安全创建一个安全的通信信道前需要检查服务身份。

  当一个服务的MEX终结点被调用来生成WSDL,它返回服务的身份。如果绑定支持WS-Security协议(所有预定义绑定支持这个协议,除了basicHttpBinding),WSDL将包含服务身份信息。依赖绑定和服务认证架构,不同的身份信息会返回。

  当使用svcutil从一个运行着的服务来生成一个客户端代理和客户端配置文件时,服务身份被写到配置文件中。在运行时,服务身份被验证来确保客户端正在通信的是合适的服务。如果运行时服务与客户端期待的身份是不同的,WCF将不会建立安全信道。

  列表8.11 显示了使用wsHttpBinding和基于客户端证书的消息层安全的客户端认证以及由svcutil为一个服务生成的配置文件。注意也包含了服务加密证书。如果客户端尝试初始化一个与服务端通信的安全连接但是服务没有对应证书,WCF将会抛出一个错误。

列表8.11 为基于证书认证生成的服务身份

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IStockService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="true" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="">
                            <extendedProtectionPolicy policyEnforcement="Never" />
                        </transport>
                        <message clientCredentialType="Certificate" negotiateServiceCredential="true"
                            algorithmSuite="Default" establishSecurityContext="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8000/EssentialWCF" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IStockService" contract="localhost.IStockService"
                name="WSHttpBinding_IStockService">
                <identity>
                    <certificate encodedValue="AwAAAAEAAAAUAAAAQTFTEGK55mOxFWsNrJscy
                                 f79hBYgAAAAAQAAAAACAAAwggH8MIIBZaADAgECAhDiu9KOnV8h
                                 mU8Z66OkxszJMA0GCSqGSIb3DQEBBAUAMBYxFDASBgNVBAMTC1d
                                 DRlNlcnZlclBLMB4XDTEwMDIyMzA4NTAyOFoXDTM5MTIzMTIzNT
                                 k1OVowFjEUMBIGA1UEAxMLV0NGU2VydmVyUEswgZ8wDQYJKoZIh
                                 vcNAQEBBQADgY0AMIGJAoGBAKsGtmlX4F+SoM6xr7SWgz6BQOC0
                                 EkZWpgAV01kN7C1kY7dY1dIyI2c57bUfxNsOiiIzLJo0ZPUj3nJ
                                 9FIudVRFFPJDuw4ZEfjDDT7OWz3xIYMfWxYFtqwpDAe9tKgvDPb
                                 jAXXJjIXbbjYqdawKQxF0a8kIgOqbAzSGAgcbPJYSZAgMBAAGjS
                                 zBJMEcGA1UdAQRAMD6AEFGF7EKlpfwVQOdLr8IZRMehGDAWMRQw
                                 EgYDVQQDEwtXQ0ZTZXJ2ZXJQS4IQ4rvSjp1fIZlPGeujpMbMyTA
                                 NBgkqhkiG9w0BAQQFAAOBgQA0tDO5C32L1fSBmHNF0rHQSrVO5W
                                 NyKmLKFt42CVJSPR4J8Kq+qVIh5uIxwtVp4iVH21JLB8QM8U4UG
                                 REAM4VFytSfr0HylGNcMoxXNoMsZ59OKrw/Ov2N/vpI8LiClAeU
                                 7Vl6ZK0dRxGUMJN5ZVGUtEMC+FpNCoq9+wVatMwvIw==" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

  列表8.12显示了使用svcutil, wsHttpBinding和基于Windows客户端认证为一个客户端生成的配置文件。注意也包含服务的Windows认证。如果客户端尝试与一个服务端建立安全通信但是服务正运行在一个不同的Windows账户下,WCF将会抛出一个错误。这一般是由于在开发环境生成的WSDL的服务账户有很多证书,但是在生产环境使用了一个不同的Windows账户。在那个情况下,客户端配置文件必须改变来与服务新的身份匹配。

列表8.12 为基于证书认证生成的服务身份

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IStockService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="true" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="">
                            <extendedProtectionPolicy policyEnforcement="Never" />
                        </transport>
                        <message clientCredentialType="Certificate" negotiateServiceCredential="true"
                            algorithmSuite="Default" establishSecurityContext="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8000/EssentialWCF" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IStockService" contract="localhost.IStockService"
                name="WSHttpBinding_IStockService">
                <identity>
                    <userPrincipalName value="MyDomain\Me">
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

  如果客户端由于一些原因不能不能确定服务在配置的账户下运行,它将抛出一个错误。例如,如果你在线下做开发工作而且没有访问活动路径,客户端在等待确定服务证书的过程可能超时。在那个情况下,你可以将<userPrincipalName>改成<servicePricipalName>来改变服务身份并在客户端配置文件中靓啊MyDomain\Me的值改为host/localhost.