401 客户端“协商”,服务器“协商,NTLM”在调用 WCF 服务器到服务器时

Posted

技术标签:

【中文标题】401 客户端“协商”,服务器“协商,NTLM”在调用 WCF 服务器到服务器时【英文标题】:401 Client 'Negotiate', Server 'Negotiate,NTLM' When Calling WCF Server to Server 【发布时间】:2014-03-17 16:40:05 【问题描述】:

好的,我已经阅读了我能找到的关于此错误的所有主题和问题,但令人惊讶的是还没有找到解决方案。我试图在我的 IIS 托管的 WCF 服务 (.NET 4.0) 上要求 Windows 身份验证,到目前为止,该服务一直是可选的。我在服务器上有一个启用 Windows 身份验证的端点已经有一段时间了,有几个远程应用程序成功使用了它。我现在正尝试将我们的 Web 应用程序和其他使用 WCF 服务的服务器应用程序切换到此安全端点,方法是为它们提供与工作远程客户端完全相同的客户端配置,但服务器应用程序正在接收带有消息的 401:

The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'Negotiate,NTLM'.]

我为 WCF 托管站点启用了匿名和 Windows 身份验证。我开始使用的 Web 应用程序托管在与 WCF 服务不同的服务器上,并且在 ASP.NET 2.0 和 Windows Server 2008 R2 Enterprise 上运行。我已经使用 allowNtlm 创建了一个客户端行为,并将 NetworkSecurity: LAN Manager 身份验证级别设置为 Send LM & NTLM... 在客户端。在托管端,它设置为仅发送 NTLMv2 响应...我不知道这是否会影响服务器/服务处理身份验证的方式。我还尝试在客户端上将 allowedImpersonationLevel 设置为 Impersonation ,谢天谢地,这不起作用(因为不需要模拟)。对于在与 Web 应用程序相同的服务器上运行的 Windows 服务和控制台应用程序,我们似乎得到了相同的结果。

这是我的服务器配置:

<binding name="WindowsSecuredBinding">
    <security mode="Transport">
        <transport clientCredentialType="Windows" />
    </security>
</binding>
...
<service behaviorConfiguration="OMWebServices.QueueServiceBehavior"
    name="OMWebServices.QueueService">
    <endpoint address="" binding="basicHttpBinding" name="QueueEndpoint"
      bindingName="" contract="OMWebServices.IQueueService" />
    <endpoint binding="basicHttpBinding" bindingConfiguration="WindowsSecuredBinding"
      name="QueueSecuredEndpoint" contract="OMWebServices.IQueueService" />
    <endpoint address="mex" binding="mexHttpBinding" name="QueueMetadataEndpoint"
      contract="IMetadataExchange" />
  </service>
...
<behavior name="OMWebServices.QueueServiceBehavior">
    <serviceMetadata httpGetEnabled="true" />
    <serviceDebug includeExceptionDetailInFaults="true" />
</behavior>

这是客户端配置:

<endpoint address="https://.../QueueService.svc" binding="basicHttpBinding" bindingConfiguration="QueueSecuredEndpoint" behaviorConfiguration="OMServiceBehavior" contract="OMQueueService.IQueueService" name="QueueSecuredEndpoint" />

<binding name="QueueSecuredEndpoint" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
  <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
  <security mode="Transport">
    <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
    <message clientCredentialType="UserName" algorithmSuite="Default" />
  </security>
</binding>
....
<!-- The behavior I tried that didn't make a difference -->
<behavior name="OMServiceBehavior">
  <clientCredentials>
    <windows allowedImpersonationLevel="Impersonation" allowNtlm="True"/>
  </clientCredentials>
</behavior>

我的第一个问题是,这条错误消息真正告诉我什么?它说客户端方案是协商,服务器响应协商,NTLM。如果服务器提供 Negotiate 并且客户端正在使用 Negotiate,那有什么问题?

显然,第二个问题是哪里出了问题,我该如何让它发挥作用?

编辑 好吧,这很愚蠢。问题似乎是没有传递凭据。早在网站开发时,我就开始编写代码以在代码中显式设置凭据,但在此过程中,found that it was already working without explicitly setting them。所以该代码仍然被注释掉。这是在 IIS 6 上运行的。现在在 IIS 7 上运行,它似乎只有在我在代码中明确设置凭据时才有效。我可以使用 w3wp 进程的帐户自动获取它吗?

【问题讨论】:

【参考方案1】:

要回答第一个问题,错误消息准确地告诉我它说的是什么;我没有被授权。告诉我客户端身份验证方案和服务器标头的行只是额外的信息,而不是冲突的指示。实际上是确认配置正确。

在暂存环境中,问题被掩盖了,因为 WCF 服务和 Web 应用程序托管在同一台服务器上。问题是 Web 应用程序的站点默认配置为对匿名用户使用本地帐户 IUSR(或 IUSR_Server)。这是正在传递的用户(我相信它等于 CredentialCache.DefaultNetworkCredentials)。当它们在不同的服务器上时,服务器 2 上的 WCF 显然无法对服务器 1 用户进行身份验证。解决方案在 IIS 中,右键单击匿名身份验证 > 编辑...> 检查应用程序池标识(在我的情况下是域帐户)或输入特定用户的域帐户。

【讨论】:

【参考方案2】:

这只是意味着您的客户端和服务器使用不同的身份验证方案。

在您的客户端配置中,您已经设置了一个

<transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />

和消息安全

<message clientCredentialType="UserName" algorithmSuite="Default" />

因此,您可能会因此遇到错误。这些链接可能会对您有所帮助。

Ch. 7 Message and Transport Security security of basicHttpBinding

另外,在您的客户端配置中

<endpoint address="https://.../QueueService.svc" binding="basicHttpBinding" bindingConfiguration="QueueSecuredEndpoint" behaviorConfiguration="OMSServiceBehavior" contract="OMQueueService.IQueueService" name="QueueSecuredEndpoint" />

behaviorConfiguration="OMSServiceBehavior"更改行为配置 给behaviorConfiguration="OMWebServices.QueueServiceBehavior"

你也试过使用TransportCredentialOnly吗?如果没有,不妨试试这个http://msdn.microsoft.com/en-us/library/ff648505.aspx

<security mode="TransportCredentialOnly">
    <transport clientCredentialType="Windows" />
</security>

【讨论】:

谢谢密涅瓦。但它似乎告诉我身份验证方案是相同的;谈判。我的行为拼写是线程错字,很好。反正我不再使用这种行为了。对于 配置,我相信无论如何它都是该属性的默认值(除了 None 而不是 Basic256)它在那里是因为 VS 生成了它。我删除了它并得到了相同的结果。我刚刚尝试了 TransportCredentialOnly 并且它有效,但我想要 Transport 以确保所有通信的安全。这有效是什么意思?两者都使用 SSL 并且都使用相同的凭据,不是吗? @drpcken 解释了TransportTransportCredentialsOnly. 这是link 对他的回答。 @Anand 还提供 Transport 和 TransportCredentialOnly 的 explanation 还注意到客户端 webconfig 缺少一个 contract="OMQueueService.IQueueService" 应该是 contract="OMWebServices.IQueueService" 这可能是最全面的one,可能会对您有所帮助。如果您要进行互联网和传输,则需要在 IIS 上安装 SSL 证书。【参考方案3】:

我的这个错误问题与配置无关,而是特定于 a WCF Service calling another on the same machine.

因为这影响了一批通过 C# 控制台应用程序部分配置的新服务器,我通过在受影响的服务器上执行如下代码来解决它:

const string userRoot = "HKEY_LOCAL_MACHINE";
const string subkey = @"SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0";
const string keyName = userRoot + @"\" + subkey;
Registry.SetValue(keyName, "BackConnectionHostNames", hostnamesOnServer.ToArray(), RegistryValueKind.MultiString);

Windows Server 2012 不需要重新启动。

【讨论】:

【参考方案4】:

这显然也可能是您的凭据未正确传递的问题。我需要:

Client.ClientCredentials.Windows.ClientCredential.UserName = User;
Client.ClientCredentials.Windows.ClientCredential.Password = Password;

代替:

Client.ClientCredentials.UserName.UserName = User;
Client.ClientCredentials.UserName.Password = Password;

(奇怪的是,第二种方法偶尔对我有用,但并非总是如此。)

【讨论】:

【参考方案5】:

我在这里报告了完全相同的问题。用于凭据的 AD 帐户已更改密码。一旦我使用了新密码,它就开始工作了。

此错误对于密码错误的情况非常具有误导性。

【讨论】:

以上是关于401 客户端“协商”,服务器“协商,NTLM”在调用 WCF 服务器到服务器时的主要内容,如果未能解决你的问题,请参考以下文章

WCF 身份验证错误

在 Windows Server 2003 上托管 WCF Web 服务

如何禁用 WCF 数据服务的身份验证方案

.NET HttpClient 在使用 NTLM 协商时不会在对 IIS 的请求之间保持身份验证

ASP.NET Windows 身份验证向应用程序返回错误的用户

内容协商与转码