WCF 身份验证问题
-
22-07-2019 - |
题
我有一个非常简单的 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>
我希望得到一些指示,或者推动正确的方向。
解决方案
好吧,您的服务使用 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,如果您关闭了 wsHttpBinding 提供的安全性和所有其他更高级功能)。
还有一个真的很好 博客文章系列 从五个不同的典型场景中讨论 WCF 安全性的基础知识 - 非常好读!
马克
其他提示
这为您提供了传输级安全性,无需身份验证:
<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>
对于其他场景,我会看看 微软WCF示例.
WCF可以是一个真正的痛苦,当涉及到配置它。看一看 WCFSecurity 它提供良好的工作样品针对不同的配置环境。
不隶属于 StackOverflow