WCF身份验证的问题

jck*_*yes 1 .net wcf exception-handling wcf-binding wcf-security

我有一个非常简单的WCF服务,我想公开公开.我创建了这项服务并将其设置在我们的服务器上,没有太多麻烦.问题是我们能够在我们的专用网络中使用该服务,但是当我们尝试从网络外部使用它时,会抛出以下错误:

安全支持提供程序接口(SSPI)协商失败.

我做了一些研究,听起来像WCF默认使用Windows身份验证.我想改变它以不使用身份验证,但我不完全确定如何.这是我的配置现在的样子.

<system.serviceModel>
    <services>
        <service behaviorConfiguration="XX.ZZ.WebService.MyServiceBehavior"
         name="XX.ZZ.WebService.MyService">
            <endpoint  address="" binding="wsHttpBinding" contract="XX.ZZ.WebService.IMyService">
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="XX.ZZ.WebService.MyServiceBehavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

我会很感激一些指示,或者在正确的方向上轻推.

mar*_*c_s 6

好吧,您的服务使用wsHttpBinding,默认情况下需要Windows用户凭据 - 外部用户显然不会拥有该凭据.默认情况下,WCF本身并不使用Windows凭据(如您所述),但实际上这种特殊绑定(wsHttpBinding) - 其他可能默认为其他设置.

你有几个选择:

  • 配置wsHttpBinding(相当"重量级")根本不使用安全性,或者使用调用者必须提供的用户名/密码安全性
  • 使用basicHttpBinding而没有安全性(基本上就是ASMX模型)

要从wsHttpBinding完全关闭安全性,请在配置中包含此内容:

<bindings>
  <wsHttpBinding>
    <binding name="NoSecurity">
      <security mode="None" />
    </binding>
  </wsHttpBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

然后配置端点以使用该绑定配置:

<system.serviceModel>
    <services>
        <service name="XX.ZZ.WebService.MyService"
                 behaviorConfiguration="XX.ZZ.WebService.MyServiceBehavior">
           <endpoint address="" 
                     binding="wsHttpBinding" 
                     bindingConfiguration="NoSecurity" 
                     contract="XX.ZZ.WebService.IMyService">
            </endpoint>
            <endpoint address="mex" 
                      binding="mexHttpBinding" 
                      contract="IMetadataExchange" />
        </service>
    </services>
Run Code Online (Sandbox Code Playgroud)

您也可以这样做,<basicHttpBinding>而不是<wsHttpBinding>如果您愿意(使用wsHttpBinding与basicHttpBinding没有任何好处,如果您关闭了安全性以及wsHttpBinding提供的所有其他更高级的功能).

还有一个非常好的博客文章系列,从五个不同的典型场景谈论WCF安全的基础 - 优秀的阅读!