WCF身份验证问题

时间:2009-08-05 14:41:42

标签: .net wcf exception-handling wcf-security wcf-binding

我有一个非常简单的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>

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

3 个答案:

答案 0 :(得分:6)

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

您有几个选择:

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

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

<bindings>
  <wsHttpBinding>
    <binding name="NoSecurity">
      <security mode="None" />
    </binding>
  </wsHttpBinding>
</bindings>

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

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

如果您愿意,可以使用<basicHttpBinding>而不是<wsHttpBinding>执行相同操作(如果您关闭了安全性以及wsHttpBinding提供的所有其他更高级功能,则使用wsHttpBinding与basicHttpBinding没有任何好处)

还有一个非常好的blog post series,就五种不同的典型场景来说说WCF安全的基础知识 - 优秀的阅读!

马克

答案 1 :(得分:2)

WCF在配置时可能会非常痛苦。看看WCFSecurity它为不同的配置环境提供了良好的工作样本。

答案 2 :(得分:2)

这为您提供了无需身份验证的传输级别安全性:

<configuration>  <system.serviceModel>
    <services>
      <service 
          name="Microsoft.ServiceModel.Samples.CalculatorService"
          behaviorConfiguration="CalculatorServiceBehavior">
        <endpoint address=""
                  binding="wsHttpBinding"
                  bindingConfiguration="Binding1" 
                  contract="Microsoft.ServiceModel.Samples.ICalculator" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="Binding1">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>  </system.serviceModel>
</configuration>

对于其他场景,我会看一下Microsoft WCF samples

相关问题