【问题标题】:Secure WCF service with basicHttpBinding使用 basicHttpBinding 保护 WCF 服务
【发布时间】:2013-10-23 19:25:22
【问题描述】:

PC:Windows 8.1 + 所有更新,VS 2012 + 所有更新。

我创建了一个 WCF 服务,我已经对其进行了测试并且它可以工作。我想针对 Windows Phone 8 应用程序使用此服务,因此需要创建一个basicHttpBinding 服务,该服务接受用户名和密码才能访问数据。以下是我在完成在 IIS 上成功运行的服务后采取的步骤。

  1. 创建了 SSL(在此处执行步骤 http://msdn.microsoft.com/en-us/library/hh556232.aspx
  2. 更改了我的 web.config 文件,使其包含:

    <system.serviceModel>
    <services>
      <service behaviorConfiguration="NewBehavior1" name="Service">
        <endpoint address="" binding="basicHttpBinding" contract="IService"     bindingConfiguration="NewBinding1" />
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="NewBinding1">
          <security mode="TransportWithMessageCredential" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MEX">
          <serviceMetadata/>
        </behavior>
        <behavior name="NewBehavior1">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom"
          customUserNamePasswordValidatorType="Service, Services" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"     multipleSiteBindingsEnabled="true" />
    

  3. 导航到 http://localhost/Service/Service.svc 并收到 403.4 消息(没有 SSL 无法访问),这是正确的,因此我将 http 替换为 https 并且我可以查看服务页面。

  4. 知道它可以使用 https 工作,我打开 WCF 测试客户端工具并导航到相同的 URL 并收到错误(请注意,我已经缩短了一些错误并留在了主要区域)

    错误:无法从https://localhost/Service/service.svc 获取元数据如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在指定地址启用元数据发布。
    元数据包含无法解析的引用:“https://localhost/Service/service.svc”。
    无法为具有权限“localhost”的 SSL/TLS 安全通道建立信任关系。
    基础连接已关闭:无法为 SSL/TLS 安全通道建立信任关系。
    根据验证程序,远程证书无效。HTTP GET 错误

  5. 我从 IIS 导出证书并将其安装在本地计算机和用户下 受信任的根证书颁发机构。没区别

所以现在我有点迷茫,因为我尝试过的任何方法似乎都不起作用。有人可以建议吗?

【问题讨论】:

    标签: wcf windows-phone-8 wcf-security


    【解决方案1】:

    我会试一试...这种行为有几个潜在的原因。

    A) 如果您的客户端测试工具是在 IE 中构建的,并且您的 SSL 证书是自签名的,那么您总是会遇到 IE 不接受证书的问题,并将证书添加到客户端的“受信任”组中没救IE 讨厌自签名证书,并且通过 MS 的导入例程是浪费时间。

    B) 如果您的客户端测试工具是自建应用程序,您不需要将证书添加到受信任组,即使它是自签名的。但是您可能需要将此添加到您的代码中(用于测试)以避免自签名证书故障:

    System.Net.ServicePointManager.CertificatePolicy = New TrustAllCertificatePolicy()
    

    C) 假设您使用的是自签名证书,请注意创建证书的方式。在我想出这些命令之前,这对我来说是个问题:

    rem creates root authority file and cert in currentuser\root and gives it the right to sign certs 
    makecert.exe -a sha1 -n CN=CAS_Temp_Authority %Host_Authority_Cert_Name% -sr LocalMachine -ss Root -sky signature -pe -r -sk MyNewKey -cy authority  
    
    rem creates ssl cert, puts it in the currentuser\root authority and signs it based on the other certificate
    makecert.exe  -n cn=%Host_URL% %Host_Cert_Name% -is root -ic %Host_Authority_Cert_Name%  -sky exchange -pe -sv %Host_Cert_PrivateKey% -eku 1.3.6.1.5.5.7.3.1
    
    rem make the pfx file that will allow you to copy certs around with private keys
    pvk2pfx -pvk %Host_Cert_PrivateKey% -spc %Host_Cert_Name% -pfx %Host_Cert_PFX% -f
    

    您可以想象,从中生成的“授权”证书(*.cer 文件)进入您的“受信任的根...”存储,另一个交换证书进入您的本地计算机/我的存储,但您想要将其导入为 *.pfx 文件,而不是 *.cer 文件。至少这对我有用。

    最后,如果 A) 和 B) 对您没有帮助,您可以尝试将 SecurityMode 从 TransportWithMessageCredential 更改为常规 Transport,看看是否会有所不同。

    祝你好运。整理这些 WCF/SSL 问题对每个人来说都很难。

    【讨论】:

    • 谢谢。我开始将 TransportWithMessageCredentials 更改为 None 并且 http://... 有效,但 https://... 无效。因此,我运行了每个证书命令,并注意到第一个命令将证书添加到 CurrentUser 的受信任根目录。剩下的 2 个命令我不确定证书是在哪里创建的以及从哪里导出的。对不起,我是这个领域的新手,所以我不介意更多的指导帮助或指向我需要遵循的教程的链接?再次感谢
    • 有很多关于创建这些自签名证书的教程;这个还不错。 akadia.com/services/ssh_test_certificate.html 除此之外,我不知道该告诉你什么。祝你好运。
    • 谢谢,但该链接似乎是指 Apache(不是 IIS),它说我已经创建了 SSL 证书但有 2 个问题 1. 请参阅我昨天的评论(上图) 2. 我可以连接从我的 WinPhone 应用程序到服务,但总是得到“远程服务器返回错误:未找到”,但不知道为什么。我已启用日志记录,但服务文件夹中未列出任何内容。更进一步,我还获得了 StartSSL 的免费证书,但不确定它是否能减轻这种痛苦?
    • 使用 ServicePointManager.CertificatePolicy 已过时。您应该改用 ServicePointManager.ServerCertificateValidationCallback。
    【解决方案2】:

    对不起,我可能完全错了,但我注意到你这样配置你的行为:

    <behavior name="NewBehavior1">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom"
      customUserNamePasswordValidatorType="Service, Services" />
      </serviceCredentials>
    

    我想如果你想使用 https,你必须像这样配置它:

    <serviceMetadata httpsGetEnabled="true"/>enter code here
    

    (为服务元数据启用 httpS)。

    我会尝试,但我不能 100% 确定它是否适合您,具体取决于您的其余配置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 2012-06-19
      • 1970-01-01
      相关资源
      最近更新 更多