How to get HttpClient to pass credentials along with the request?

前端 未结 7 1468
南笙
南笙 2020-11-22 14:52

I have a web application (hosted in IIS) that talks to a Windows service. The Windows service is using the ASP.Net MVC Web API (self-hosted), and so can be communicated with

相关标签:
7条回答
  • 2020-11-22 15:51

    In .NET Core, I managed to get a System.Net.Http.HttpClient with UseDefaultCredentials = true to pass through the authenticated user's Windows credentials to a back end service by using WindowsIdentity.RunImpersonated.

    HttpClient client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true } );
    HttpResponseMessage response = null;
    
    if (identity is WindowsIdentity windowsIdentity)
    {
        await WindowsIdentity.RunImpersonated(windowsIdentity.AccessToken, async () =>
        {
            var request = new HttpRequestMessage(HttpMethod.Get, url)
            response = await client.SendAsync(request);
        });
    }
    
    0 讨论(0)
提交回复
热议问题