首页 > 解决方案 > Web 服务凭证从远程工作,但不在服务器上

问题描述

我正在调用一个 Web 服务(名为 SecurityService),如下所示:

SecurityService.Service securityService = new SecurityService.Service();
securityService.Credentials = networkCredential;

return securityService.GetUserToken();

该服务启用了 Windows 身份验证,仅此而已。上面的这段代码工作,来自远程机器。

当我将相同的代码推送到运行此服务的 IIS 服务器时,我收到 401 错误:

The request failed with HTTP status 401: Unauthorized.

我用 WCF 尝试了相同的代码:

ServiceSoapClient client = new ServiceSoapClient();
client.ClientCredentials.Windows.ClientCredential = networkCredential;
return client.GetUserToken();

使用这些设置:

<binding name="ServiceSoap">
  <security mode="TransportCredentialOnly">
    <transport clientCredentialType="Windows" proxyCredentialType="None" realm=""/>
  </security>
</binding>

同样,在服务器上,我无法从连接到该服务器的另一台机器上对其进行身份验证,它可以工作。

以下是 Web 服务的高级日志记录捕获的标头:

当它工作时:

sc-win32-status WWW-Authenticate Authorization
2147024891 "NTLM,Negotiate" -
0 - "Negotiate TlRMTVNTUAADAAAAGAAYAIoAAACi...."

当它失败时,从同一服务器中调用:

sc-win32-status WWW-Authenticate Authorization
2147024891 "NTLM,Negotiate" -
1073741715 "NTLM,Negotiate" "Negotiate ADAAAAGAAYAI4AAACiAaIBpgAAABoAGgBY..."

当我使用 WCF 时,我也收到了这条消息,而不是普通的 401 错误:

The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'NTLM,Negotiate'. ---> The remote server returned an error: (401) Unauthorized.

错误1073741715的解释:

STATUS_LOGON_FAILURE (-1073741715 (0xC000006D))
The attempted logon is not valid. This is due to either an incorrect user name or incorrect authentication information.

不知何故,当我的代码从服务器内运行时,它没有正确地对 Web 服务进行身份验证。

这是指向同一问题的链接。答案并不适用:

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

标签: c#authenticationwindows-authenticationcredentialsntlm

解决方案


您需要向与您的 IIS 应用程序相关的 IIS Web 应用程序或 IIS 应用程序池提供您的用户凭据。

为了这

  • 打开 IIS。
  • 单击应用程序池
  • 选择您的应用程序在其上运行的应用程序池
  • 单击右窗格中的高级属性
  • 在 Process Model 下找到 Identity 并单击...按钮
  • 单击自定义帐户并在那里添加您的 Windows 帐户
  • 单击确定并保存。

还要确保应用程序不使用自定义用户。或者单击 IIS 应用程序,然后单击右窗格中的高级设置。之后修改物理路径凭据物理路径凭据登录类型


推荐阅读