【发布时间】:2018-03-02 23:37:07
【问题描述】:
请帮忙! 我需要我的 asp 应用程序来请求具有模拟用户凭据的远程系统。但总是得到 401 Unauthorized 错误。 我从这里进行了所有配置:https://support.microsoft.com/en-us/help/810572/how-to-configure-an-asp-net-application-for-a-delegation-scenario Kerberos 已在我的应用程序和我的测试远程应用程序中配置并运行(我在提琴手中看到了 kerberos 票证)。委派、spns 等一切都已配置完毕。
这是我使用 System.Net.Http.httpclient 的代码:
HttpClientHandler handler = new HttpClientHandler()
{
UseDefaultCredentials = true,
PreAuthenticate = true
};
using (HttpClient client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Accept.Clear();
var method = new HttpMethod("GET");
var request = new HttpRequestMessage(method, "http://testdelegationapp.avp.ru/");
var response = client.SendAsync(request).Result;
}
实际上 http 请求是由 Apppool 帐户发出的(在远程应用 IIS 中限制对 Apppool 帐户的访问时出现 401 错误)
这里:How to get HttpClient to pass credentials along with the request? 声称HttpClient不能将安全令牌传递给另一个线程,最好使用System.Net.WebClient的同步方法
使用 webclient 的代码:
var wi = (WindowsIdentity)HttpContext.User.Identity;
var wic = wi.Impersonate();
try
{
string URI = "http://testdelegationapp.avp.ru/";
using (WebClient wc = new WebClient())
{
wc.UseDefaultCredentials = true;
string response = wc.DownloadString(URI);
}
}
finally
{
wic.Undo();
}
结果更糟,同样的 401 错误,但在 fiddler 中我可以看到 webclient 使用 NTLM 票证访问远程应用程序!
从这里配置流动令牌抛出线程:Unable to authenticate to ASP.NET Web Api service with HttpClient 也无济于事。 SecurityContext.IsWindowsIdentityFlowSuppressed() 为假。
WindowsIdentity.GetCurrent().Name 和 Thread.CurrentPrincipal.Identity.Name 显示应有的模拟用户。
【问题讨论】:
标签: httpclient webclient kerberos impersonation delegation