【发布时间】:2011-04-13 21:10:04
【问题描述】:
我已使用以下配置设置 WCF 服务以要求 NTLM 身份验证:
<system.serviceModel>
<bindings>
<customBinding>
<binding name="BinarySecurityBinding">
<binaryMessageEncoding/>
<httpTransport authenticationScheme="Ntlm"/>
</binding>
</customBinding>
</bindings>
<services>
<service name="Services.LogisticsServices" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="customBinding" bindingConfiguration="BinarySecurityBinding" contract="Services.ILogisticsServices" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
我这样做是为了强制使用 Web 服务的应用程序登录,因为我的所有服务操作都使用模拟 ([OperationBehavior(Impersonation = ImpersonationOption.Required)])。
在 IIS 7 中,我启用了匿名和 Windows 身份验证。
当我访问托管上述服务的http://test.server/LogisticsServices.svc 时,我可以匿名看到默认的服务描述页面。但是,当 Visual Studio 尝试访问 http://test.server/LogisticsServices.svc/$metadata 以生成客户端代理时,服务器正在响应 HTTP 代码 401 并期待身份验证。我不仅希望元数据可以匿名使用,而且服务器不接受我提供的凭据(尽管我知道它们是正确的)。
测试不同的配置,我尝试从绑定的传输中删除 authenticationScheme,只是为了能够生成客户端代理,但这会导致异常,因为服务的操作需要模拟 ([OperationBehavior(Impersonation = ImpersonationOption.Required)])。
我的服务配置中遗漏了什么可以匿名使用服务的元数据?如果我在处理整个事情时出错了,我也愿意接受建议。
【问题讨论】:
-
我已经用类似的配置配置了一个测试服务,并且在获取元数据时没有问题。在浏览器中,您可以通过输入以下内容来查看 wsdl:test.server/LogisticsServices.svc?wsdl
-
我可以看到 WSDL。我注意到只有在 .svc 后有斜杠时才需要凭据,例如test.server/LogisticsServices.svc 或者,当然,test.server/LogisticsServices.svc/$metadata
-
如果您可以获得 WSDL,那么一个尴尬但简单的解决方案是创建一个包含 WSDL 的文件(以及每个 wsdl:import 引用内容的文件)并将其放置在开发环境中的 Web 服务器上.使用文件的 URL 构建代理。每次服务合同更改(尴尬)时,您都必须更新 WSDL 和相关文件,但它确实解决了您的身份验证问题 :)
标签: wcf wcf-security