【问题标题】:WCF - Encyrption (and possibly authentication) using SSLWCF - 使用 SSL 加密(可能还有身份验证)
【发布时间】:2011-08-17 04:39:06
【问题描述】:

这是我的场景和问题:

  • 我们有一个 Windows 2008 R2 服务器 (IIS7)
  • SSL 认证
  • .NET 4

我尝试在网上寻找一个好的资源,但它们中的大多数都不完整并且彼此之间存在足够的差异,以至于它们并没有真正混合搭配。我希望使用 WCF 服务从我的客户端/服务器加密传递消息。我还将实现一些自定义身份验证方案。身份验证方案将验证用户/通过第一次身份验证。然后从那时起,客户端将使用随机生成的代码作为他们的身份验证。

根据我收集到的信息,我需要执行以下操作:

  • [ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
  • 使用 WSHttpBinding

这就是我有点不知所措的地方。有很多资料表明服务器和客户端的配置文件应该如何,我真的很困惑。我的服务器/客户端配置文件应该是什么样子,以便我的服务发送/接收加密的消息并可以指向自定义身份验证?


或者,如果我不能将此自定义身份验证用作 WCF 客户端对象的参数,我将只将身份验证凭据作为消息本身的一部分传递,只要消息是加密的。

如果有一个实际的、完全完整的(即不是部分信息)资源,那就太好了。或者,如果有人知道必要的客户端/服务器 App.config/Web.config 设置来使用,那也很棒。

【问题讨论】:

    标签: wcf authentication ssl web-config app-config


    【解决方案1】:

    我相信您想使用 HTTPS 来确保传输级别的加密和签名。您需要先使用 SSL 证书配置 IIS7.5,并在托管服务的应用程序中允许 https。检查this tutorial,但您将使用现有证书而不是创建自签名证书。您的证书应该为您网站的主机标头(例如 mydomain.com)或您的服务器名称(如果直接公开)创建。

    在您的服务中,您需要具有传输安全性的 basicHttpBinding 和用于身份验证的消息凭据。

    <bindings>
      <basicHttpBinding>
        <binding name="secured">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="securedService">
        <serviceMetadata httpsGetEnabled="true" />
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" userNamePasswordValidator="..." />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="..." behaviorConfiguration="securedService">
        <endpoint address="" contract="..." binding="basicHttpBinding" bindingConfiguration="secured" />
        <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpsBinding" />
      </service>
    </services>
    

    客户端将使用类似的设置:

    <bindings>
      <basicHttpBinding>
        <binding name="secured">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint name="..." address="https://..." contract="..." binding="basicHttpBinding" bindingConfiguration="secured" />
    </client>
    

    此配置定义符合 HTTPS 传输并受用户名和密码保护的 SOAP 1.1 服务。服务还通过 HTTPS 公开其元数据 (WSDL)。用户名和密码由自定义密码验证器验证(您必须implement one)。

    您将在客户端设置凭据(由添加服务引用生成):

    var client = new MyServiceClient();
    client.ClientCredentials.UserName.UserName = "Name";
    client.CleintCredentials.UserName.Password = "Password";
    

    消息的加密是在传输层完成的。每次创建新代理时都必须配置凭据,但它们会重复用于来自代理的所有调用。

    您的方案可能不需要 WsHttpBinding。设置 ProtectionLevel 仅用于消息级别的安全性 - 我认为这不是您想要的。

    不要使用您描述的自定义身份验证:

    身份验证方案将验证 用户/第一次通过 验证。那么从那时起 客户端将使用随机生成的 代码作为他们的身份验证。

    这要复杂得多。您将推出自己的非标准解决方案,或者您将使用 WCF 的内置实现(安全对话),但并非每个客户端 SOAP 堆栈都能够使用此类服务​​(它完全依赖于高级消息安全性)。

    【讨论】:

    • 我不需要使用 WSHttpBinding,因为它会加密消息而不是以明文形式发送消息:"One of the biggest differences you must have noticed is the security aspect. By default, BasicHttpBinding sends data in plain text while WsHttpBinding sends it in encrypted and secured manner." (codeproject.com/KB/WCF/HttpBinding.aspx)
    • 默认情况下,但我不使用默认配置 - 我使用传输安全性创建了自定义 basicHttpBidning 配置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多