kerberos委托方案中的ASP双跳请求

时间:2017-09-21 14:43:24

标签: httpclient webclient kerberos impersonation delegation

请帮忙! 我需要我的asp应用程序来请求具有模拟用户凭据的远程系统。但总是得到401 Unauthorized错误。 我从这里做了所有配置:https://support.microsoft.com/en-us/help/810572/how-to-configure-an-asp-net-application-for-a-delegation-scenario Kerberos已配置并在我的应用程序和我的测试远程应用程序中工作(我在fiddler中看到kerberos票证)。委派,spns和所有内容都已配置。

这是我的代码usnig 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中,我可以看到使用NTLM票证的webclient到达远程应用程序!

配置流动令牌从这里抛出线程:Unable to authenticate to ASP.NET Web Api service with HttpClient 也没有帮助。 SecurityContext.IsWindowsIdentityFlowSuppressed()为false。

WindowsIdentity.GetCurrent()。Name和Thread.CurrentPrincipal.Identity.Name按模样显示模拟用户。

1 个答案:

答案 0 :(得分:2)

所有时间问题都在Chrome浏览器中,默认情况下它会激活kerberos委托。您应该将以下内容添加到注册表中:

Path: HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome ; 
string: AuthNegotiateDelegateWhitelist ; 
value: *.avp.ru

所以,现在我的工作配置是

用于网络请求的HttpClient。

如果您想要执行所有应用程序,则在IIS中进行ASP模拟 委托凭证。如果您想要特定于方法的委派,请使用:

var wi = (WindowsIdentity)HttpContext.User.Identity;    
var wic = wi.Impersonate();
wic.Undo();

HttpClient在另一个线程中执行请求,因此在aspnet_config.config中我们需要进行以下更改:

<legacyImpersonationPolicy enabled="false"/>
<alwaysFlowImpersonationPolicy enabled="true"/>

您可以在

中找到aspnet_config.config
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet.config
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet.config
C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet.config
相关问题