关于客户端应用程序中WCF安全性的问题

时间:2010-09-16 18:06:56

标签: wcf security wcf-security wcf-client x509certificate

我要做的是在另一台服务器上设置对服务的调用。 到目前为止..我已经创建了代理并获得了配置信息。

我遇到的问题是如何设置安全性。他们正在使用邮件安全和客户端证书。

这是我的app.config文件..到目前为止我有什么。有关设置安全性的任何信息都会有所帮助。我遇到的大多数示例都与设置服务并在托管端保护它有关。

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="CCaRWebServiceSoap11Binding" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="01:00:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
        <customBinding>
            <binding name="CCaRWebServiceSoap12Binding">
                <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                    messageVersion="Soap12" writeEncoding="utf-8">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                </textMessageEncoding>
                <httpTransport manualAddressing="false" maxBufferPoolSize="524288"
                    maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
                    realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                    useDefaultWebProxy="true" />
            </binding>
        </customBinding>
    </bindings>
    <client>
        <endpoint address="serviceEndpoint1address/"
            binding="basicHttpBinding" bindingConfiguration="CCaRWebServiceSoap11Binding"
            contract="CCaRWebServicePortType" name="CCaRWebServiceHttpSoap11Endpoint" />
        <endpoint address="serviceEndpoint2address/"
            binding="customBinding" bindingConfiguration="CCaRWebServiceSoap12Binding"
            contract="CCaRWebServicePortType" name="CCaRWebServiceHttpSoap12Endpoint" />
    </client>
</system.serviceModel>

我有点投入到这个项目中,所以WCF对我来说有些陌生。

1 个答案:

答案 0 :(得分:2)

您的项目中是否有服务参考?您的服务是否在WSDL中提供安全性描述?如果两个问题的答案都是真的,您只需更新服务参考,您的配置就会更改为安全模式(如果幸运的话)。

对您来说,真正意味着什么是安全保障?消息安全性还可以指消息加密和签名。 Basic Http Binding不支持消息安全性。对于自定义绑定,您可以从以下配置开始:

<customBinding> 
  <binding name="CCaRWebServiceSoap12Binding"> 
    <security authenticationMode="MutualCertificate" /> 
    <!-- there is plenty other configuration attributes in security element - 
         you simply have to know what you need -->
    ...
  </binding>
</customBinding>

这将为您的服务和客户端设置mutal证书身份验证(具有非对称安全性)。您需要具有私钥的服务证书和客户端证书(由您的服务提供商提供)。您需要将这些证书导入认证商店。运行客户端应用程序的帐户必须能够访问客户端证书的私钥(如果将证书放入用户的个人存储中,则应该是自动的)。

您将在端点行为中设置这些证书:

<behaviors>
  <endpointBehaviors>
    <behavior name="clientBehavior">
      <clientCredentials>
        <clientCertificate findValue="..." storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectName" /> 
        <serviceCertificate>
          <defaultCertificate findValue="..." storeLocation="CurrentUser" storeName="TrustedPeople" x509FindType="FindBySubjectName" />
        </serviceCertificate>
      </clientCredentials>
    </behavior>
  </endpointBehaviors>
</behaviors>

在端点中,您将引用此行为:

<endpoint address="serviceEndpoint2address/" binding="customBinding" 
  bindingConfiguration="CCaRWebServiceSoap12Binding"        
  contract="CCaRWebServicePortType" name="CCaRWebServiceHttpSoap12Endpoint"
  behaviorConfiguration="clientBehavior" />

您还可以从代理实例上的代码设置这些证书。

请注意,这只是许多设置中的一个。我并不是说它“按原样”对你有用。使用证书设置消息安全性非常棘手,尤其是如果您在WSDL中没有安全描述或者服务未在WCF中编写。

你也可以在MSDN上阅读这篇文章。它还配置客户端。