Windows 身份验证 Web API 未经授权 - 多个服务器
Windows Authentication Web API Unauthorized - Multiple Servers
场景
我在同一内部网络中的不同服务器上有两个应用程序 运行。
服务器 1 包含一个 WebAPI MVC 项目。
服务器 2 包含一个调用服务器 1 上的 API 的 MVC Web 应用程序。
两个应用程序都使用 Windows 身份验证。
当我通过 IIS 和 运行 通过 Visual Studio.
在本地 运行 他们时,代码工作正常
在服务器上 运行 时出现以下错误:
The remote server returned an error: (401) Unauthorized.
下面是Fiddler的请求。
GET http://server1/..../..../GetTest HTTP/1.1
Host: server 1
Connection: keep-alive
Cache-Control: max-age=0
Authorization: Negotiate YIIHIgYGKwYBBQUCoIIHFjCCBxK.....
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Cookie: ASP.NET_SessionId=gffyzdgub41op0diygy5lyv2
下面是API请求码
public string GetTest()
{
string sURL = "http://server2/.../api/test?value=JERONIMO";
WebRequest wrGETURL = WebRequest.Create(sURL);
wrGETURL.Credentials = CredentialCache.DefaultCredentials;
var result = "";
using (Stream objStream = wrGETURL.GetResponse().GetResponseStream()) {
using(StreamReader objReader = new StreamReader(objStream)) {
string sLine = "";
int i = 0;
while (sLine != null) {
i++;
sLine = objReader.ReadLine();
if (sLine != null) result += sLine;
}
}
}
return result;
}
感谢您的帮助。
使用名为 double hop.
的 Windows 身份验证时,这是一个众所周知的问题
2个此类问题的解决方案:
将 Server2
上的 Application pool identity 从 ApplicationPoolIdentity 更改为服务帐户,这样我们就可以调用 Server1
。当用户在 Server2
上调用 api 时,此解决方案不会传播上下文(您仍然可以通过向 Server1
方法发送参数化信息来调用以过滤数据)。
实施constrained delegation允许Server2
将用户身份转发给Server1
;除非您对此有强烈的安全要求,否则我会采用第一种方法。
场景
我在同一内部网络中的不同服务器上有两个应用程序 运行。
服务器 1 包含一个 WebAPI MVC 项目。
服务器 2 包含一个调用服务器 1 上的 API 的 MVC Web 应用程序。
两个应用程序都使用 Windows 身份验证。
当我通过 IIS 和 运行 通过 Visual Studio.
在本地 运行 他们时,代码工作正常在服务器上 运行 时出现以下错误:
The remote server returned an error: (401) Unauthorized.
下面是Fiddler的请求。
GET http://server1/..../..../GetTest HTTP/1.1
Host: server 1
Connection: keep-alive
Cache-Control: max-age=0
Authorization: Negotiate YIIHIgYGKwYBBQUCoIIHFjCCBxK.....
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Cookie: ASP.NET_SessionId=gffyzdgub41op0diygy5lyv2
下面是API请求码
public string GetTest()
{
string sURL = "http://server2/.../api/test?value=JERONIMO";
WebRequest wrGETURL = WebRequest.Create(sURL);
wrGETURL.Credentials = CredentialCache.DefaultCredentials;
var result = "";
using (Stream objStream = wrGETURL.GetResponse().GetResponseStream()) {
using(StreamReader objReader = new StreamReader(objStream)) {
string sLine = "";
int i = 0;
while (sLine != null) {
i++;
sLine = objReader.ReadLine();
if (sLine != null) result += sLine;
}
}
}
return result;
}
感谢您的帮助。
使用名为 double hop.
的 Windows 身份验证时,这是一个众所周知的问题2个此类问题的解决方案:
将
Server2
上的 Application pool identity 从 ApplicationPoolIdentity 更改为服务帐户,这样我们就可以调用Server1
。当用户在Server2
上调用 api 时,此解决方案不会传播上下文(您仍然可以通过向Server1
方法发送参数化信息来调用以过滤数据)。实施constrained delegation允许
Server2
将用户身份转发给Server1
;除非您对此有强烈的安全要求,否则我会采用第一种方法。