【问题标题】:Question on WCF Security in a Client Application关于客户端应用程序中 WCF 安全性的问题
【发布时间】:2010-09-16 18:06:55
【问题描述】:

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

我找不到的是如何设置安全性。他们正在使用消息安全和客户端证书。

这是我的 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 对我来说有点陌生。

【问题讨论】:

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


    【解决方案1】:

    您的项目中有服务参考吗?您的服务是否在 WSDL 中提供安全描述?如果这两个问题的答案都是正确的,您可以简单地更新服务参考,您的配置将更改为安全模式(如果您幸运的话)。

    对您而言,信息安全实际上意味着什么?消息安全也可能意味着消息加密和签名。基本 Http 绑定不支持消息安全性。对于自定义绑定,您可以从以下配置开始:

    <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>
    

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

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

    <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 上查看这篇文章。它还配置客户端。

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 2011-04-25
      • 2016-03-20
      • 1970-01-01
      • 2012-07-24
      相关资源
      最近更新 更多