我的 ASP.NET Web 应用程序在我们的 Intranet 上使用 Windows 身份验证。我希望它能够向同一域上的另一台服务器发出服务器端 http 请求,该服务器也需要 Windows 身份验证。
在此处提出附加请求时,我已按照有关临时模拟经过身份验证的用户的说明进行操作:
http://msdn.microsoft.com/en-us/library/ff647404.aspx
使用这样的代码:
using System.Security.Principal;
// Obtain the authenticated user's Identity
WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = null;
try
{
// Start impersonating
ctx = winId.Impersonate();
// Now impersonating
// Access resources using the identity of the authenticated user
var request = WebRequest.Create("http://intranet/secureapp");
request.Credentials = CredentialCache.DefaultCredentials;
var response = request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
Response.Write(streamReader.ReadToEnd());
}
}
// Prevent exceptions from propagating
catch
{
}
finally
{
// Revert impersonation
if (ctx != null)
ctx.Undo();
}
// Back to running under the default ASP.NET process identity
但是,不幸的是,我总是收到 401 未经授权的错误。
我是否需要使用活动目录配置我们的网络服务器以允许它委派经过身份验证的用户(可能是大约 200 个用户中的任何一个,所以不想做任何事情 200 次:))?如果是这样,谁能告诉我该怎么做?