203

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 over http using JSON. The web application is configured to do impersonation, the idea being that the user who makes the request to the web application should be the user that the web application uses to make the request to the service. The structure looks like this:

(The user highlighted in red is the user being referred to in the examples below.)


The web application makes requests to the Windows service using an HttpClient:

var httpClient = new HttpClient(new HttpClientHandler() 
                      {
                          UseDefaultCredentials = true
                      });
httpClient.GetStringAsync("http://localhost/some/endpoint/");

This makes the request to the Windows service, but does not pass the credentials over correctly (the service reports the user as IIS APPPOOL\ASP.NET 4.0). This is not what I want to happen.

If I change the above code to use a WebClient instead, the credentials of the user are passed correctly:

WebClient c = new WebClient
                   {
                       UseDefaultCredentials = true
                   };
c.DownloadStringAsync(new Uri("http://localhost/some/endpoint/"));

With the above code, the service reports the user as the user who made the request to the web application.

What am I doing wrong with the HttpClient implementation that is causing it to not pass the credentials correctly (or is it a bug with the HttpClient)?

The reason I want to use the HttpClient is that it has an async API that works well with Tasks, whereas the WebClient's asyc API needs to be handled with events.

j0k
  • 22,600
  • 28
  • 79
  • 90
adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • Possible duplicate of http://stackoverflow.com/q/10308938/1045728 – Tommy Grovnes Aug 31 '12 at 09:27
  • It seems that HttpClient and WebClient consider different things to be DefaultCredentials. Did you try HttpClient.setCredentials(...) ? – Germann Arlington Aug 31 '12 at 09:43
  • BTW, WebClient has `DownloadStringTaskAsync` in .Net 4.5, which can also be used with async/await – L.B Aug 31 '12 at 09:50
  • @L.B: we cannot upgrade to .Net 4.5 (yet), so for now I am stuck with the .Net 4.0 implementation. – adrianbanks Aug 31 '12 at 09:51
  • 1
    @GermannArlington: `HttpClient` doesn't have a `SetCredentials()` method. Can you point me to what you mean? – adrianbanks Aug 31 '12 at 09:54
  • HttpClientHandler does. http://blogs.msdn.com/b/henrikn/archive/2012/08/07/httpclient-httpclienthandler-and-httpwebrequesthandler.aspx - I actually meant to put HttpClientHandler.setCredentials(...) in the original comment but copied wrong class name – Germann Arlington Aug 31 '12 at 09:59
  • @GermannArlington: Ah, ok. I cannot set the credentials explicitly using that call as it requires an `ICredentials` objects which I don't have as I'm using Windows Authentication. – adrianbanks Aug 31 '12 at 10:26
  • 4
    It would appear this has been fixed (.net 4.5.1)? I tried creating `new HttpClient(new HttpClientHandler() { AllowAutoRedirect = true, UseDefaultCredentials = true }` on a web server accessed by a Windows-authenticated user, and the web site did authenticate for another remote resource after that (would not authenticate without the flag set). – GSerg Jan 27 '15 at 17:37

9 Answers9

187

You can configure HttpClient to automatically pass credentials like this:

var myClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
Aage
  • 5,932
  • 2
  • 32
  • 57
Sean
  • 2,531
  • 2
  • 17
  • 17
  • 19
    I know how to do that. The behaviour is not what I want (as stated in the question) - "This makes the request to the Windows service, but does not pass the credentials over correctly (the service reports the user as IIS APPPOOL\ASP.NET 4.0). This is not what I want to happen." – adrianbanks Apr 15 '13 at 16:50
  • 6
    this seems to fix my issue where iis only has windows authentication enabled. if you just need some legit credentials passed, this should do it. – Timmerz Jan 22 '14 at 16:06
  • Not sure this works the same as WebClient in impersonation/delegation scenarios. I get "The target principal name is incorrect" when using HttpClient with the above solution, but using WebClient with a similar setup passes the user's credentials through. – Peder Rice Sep 20 '16 at 20:43
  • This did work for me and the logs show correct user. Although, with double hop in the picture, I did not expect it to work with NTLM as the underlying authentication scheme, but it works. – Nitin Rastogi Jul 12 '17 at 13:49
  • 1
    Why does this work differently than System.Net.CredentialCache.DefaultCredentials or System.Net.CredentialCache.DefaultNetworkCredentials? – tstrand66 Dec 14 '21 at 22:08
76

I was also having this same problem. I developed a synchronous solution thanks to the research done by @tpeczek in the following SO article: Unable to authenticate to ASP.NET Web Api service with HttpClient

My solution uses a WebClient, which as you correctly noted passes the credentials without issue. The reason HttpClient doesn't work is because of Windows security disabling the ability to create new threads under an impersonated account (see SO article above.) HttpClient creates new threads via the Task Factory thus causing the error. WebClient on the other hand, runs synchronously on the same thread thereby bypassing the rule and forwarding its credentials.

Although the code works, the downside is that it will not work async.

var wi = (System.Security.Principal.WindowsIdentity)HttpContext.Current.User.Identity;

var wic = wi.Impersonate();
try
{
    var data = JsonConvert.SerializeObject(new
    {
        Property1 = 1,
        Property2 = "blah"
    });

    using (var client = new WebClient { UseDefaultCredentials = true })
    {
        client.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8");
        client.UploadData("http://url/api/controller", "POST", Encoding.UTF8.GetBytes(data));
    }
}
catch (Exception exc)
{
    // handle exception
}
finally
{
    wic.Undo();
}

Note: Requires NuGet package: Newtonsoft.Json, which is the same JSON serializer WebAPI uses.

Community
  • 1
  • 1
Joshua
  • 4,099
  • 25
  • 37
  • 1
    I did something similar in the end, and it works really well. The asynchronous issue is not a problem, as I want the calls to block. – adrianbanks Oct 01 '12 at 14:51
  • WebClient is vastly different from HttpClient and ultimately WebClient is more limited (if you POST using UploadValues, for instance, you can't get a Stream response). The HttpClient based solution is better if you are already using or need to use HttpClient. – user169771 May 14 '21 at 16:21
32

What you are trying to do is get NTLM to forward the identity on to the next server, which it cannot do - it can only do impersonation which only gives you access to local resources. It won't let you cross a machine boundary. Kerberos authentication supports delegation (what you need) by using tickets, and the ticket can be forwarded on when all servers and applications in the chain are correctly configured and Kerberos is set up correctly on the domain. So, in short you need to switch from using NTLM to Kerberos.

For more on Windows Authentication options available to you and how they work start at: http://msdn.microsoft.com/en-us/library/ff647076.aspx

BlackSpy
  • 5,563
  • 5
  • 29
  • 38
  • BlackSpy is right, you're basically describing a delegation scenario which is something the Windows Indentity Foundation handles as [described in this article](http://msdn.microsoft.com/en-us/library/ee517269.aspx) – Sixto Saez Aug 31 '12 at 20:35
  • 3
    "_NTLM to forward the identity on to the next server, which it cannot do_" - how come it does do this when using `WebClient`? This is the thing I don't understand - if it is not possible, how come it _is_ doing it? – adrianbanks Sep 02 '12 at 23:02
  • 2
    When using web client it is still only one connection, between the client and the server. It can impersonate the user on that server (1 hop), but can't forward those credentials on to another machine (2 hops - client to server to 2nd server). For that you need delegation. – BlackSpy Sep 03 '12 at 20:26
  • @BlackSpy: I don't understand your response. With `WebClient`, the credentials received by the web service are those of user X. When using the `HttpClient`, the credentials are the app pool of IIS. I want the `WebClient` behaviour but in the `HttpClient`. – adrianbanks Sep 04 '12 at 22:46
  • 1
    The only way to accomplish what you are trying to do in the manner you are trying to do it is to get the user to type his username and password into a custom dialog box on your ASP.NET application, store them as strings and then use them to set your identity when you connect to your Web API project. Otherwise you need to drop NTLM and move to Kerberos, so that you can pass the Kerboros ticket across to the Web API project. I highly recommend reading the link I attached in my original answer. What you are trying to do requires a strong understanding of windows authentication before you begin. – BlackSpy Sep 06 '12 at 12:00
  • 3
    @BlackSpy: I have plenty of experience with Windows Authentication. What I am trying to understand is why the `WebClient` can pass on the NTLM credentials, but the `HttpClient` cannot. I can achieve this using ASP.Net impersonation alone, and not having to use Kerberos or to store usernames/passwords. This however *only* works with `WebClient`. – adrianbanks Sep 12 '12 at 14:56
  • 1
    It should be impossible to impersonate across more than 1 hop without passing the username and password around as text. it breaks the rules of Impersonation, and NTLM will not allow it. WebClient allows you to jump 1 hop because you pass up the credentials and run as that user on the box. If you look at the security logs you will see the login - the user logs into the system. You can't then run as that user from that machine unless you've passed the credentials as text and use another webclient instance to log onto the next box. – BlackSpy Sep 13 '12 at 15:13
  • 1
    When you say "UseDefaultCredentials = true", NTLM is preventing you from passing the credentials on to the next server in the chain. Only Kerberos is allowed to do this. – BlackSpy Sep 13 '12 at 15:19
  • I finally made it. This is two great articles that helped me : http://bugfree.dk/blog/2016/05/18/lessons-learned-setting-up-an-iis-web-application-for-double-hop-kerberos-authentication-with-delegation https://blogs.msdn.microsoft.com/friis/2009/12/31/things-to-check-when-kerberos-authentication-fails-using-iisie/ – Pak Jun 20 '18 at 07:18
  • I think the answer is that you are not actually using NTLM when credentials are being forwarded on to a second server whether you realize it or not. If it all of a sudden started working for you its because of Kerberos. Kerberos is now the default configuration for AD domains and has been available in ever evolving forms within AD since Server 2003. That being the case NTLM is also present on most implementations of AD as a fallback option should the Kerberos protocol fail to resolve a request. So before you continuing to troubleshoot this problem ensure Kerberos is configured correctly in AD! – Nathan Nov 07 '18 at 21:50
  • How do you force HttpClientHandler to use Kerberos and not NTLM? – Arrow_Raider Aug 25 '20 at 22:13
  • @Arrow_Raider It's not a function of the HttpClient, it's configured in the operating system/domain, normally through Group Policy. – BlackSpy Aug 25 '20 at 22:49
24

OK, so thanks to all of the contributors above. I am using .NET 4.6 and we also had the same issue. I spent time debugging System.Net.Http, specifically the HttpClientHandler, and found the following:

    if (ExecutionContext.IsFlowSuppressed())
    {
      IWebProxy webProxy = (IWebProxy) null;
      if (this.useProxy)
        webProxy = this.proxy ?? WebRequest.DefaultWebProxy;
      if (this.UseDefaultCredentials || this.Credentials != null || webProxy != null && webProxy.Credentials != null)
        this.SafeCaptureIdenity(state);
    }

So after assessing that the ExecutionContext.IsFlowSuppressed() might have been the culprit, I wrapped our Impersonation code as follows:

using (((WindowsIdentity)ExecutionContext.Current.Identity).Impersonate())
using (System.Threading.ExecutionContext.SuppressFlow())
{
    // HttpClient code goes here!
}

The code inside of SafeCaptureIdenity (not my spelling mistake), grabs WindowsIdentity.Current() which is our impersonated identity. This is being picked up because we are now suppressing flow. Because of the using/dispose this is reset after invocation.

It now seems to work for us, phew!

Liam
  • 27,717
  • 28
  • 128
  • 190
Chullybun
  • 241
  • 2
  • 2
  • 2
    Thank you so much for doing this analysis. This fixed my situation too. Now my Identity is passed across correctly to the other web application! You saved me hours of work! I'm surprised it isn't higher on the tick count. – justdan23 Jun 28 '18 at 20:22
  • I only needed `using (System.Threading.ExecutionContext.SuppressFlow())` and the issue was resolved for me! – ZX9 Apr 13 '20 at 20:08
15

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);
    });
}
scourge192
  • 1,909
  • 16
  • 22
5

It worked for me after I set up a user with internet access in the Windows service.

In my code:

HttpClientHandler handler = new HttpClientHandler();
handler.Proxy = System.Net.WebRequest.DefaultWebProxy;
handler.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
.....
HttpClient httpClient = new HttpClient(handler)
.... 
adrianbanks
  • 81,306
  • 22
  • 176
  • 206
3

Ok so I took Joshoun code and made it generic. I am not sure if I should implement singleton pattern on SynchronousPost class. Maybe someone more knowledgeble can help.

Implementation

//I assume you have your own concrete type. In my case I have am using code first with a class called FileCategory
FileCategory x = new FileCategory { CategoryName = "Some Bs"};
SynchronousPost<FileCategory>test= new SynchronousPost<FileCategory>();
test.PostEntity(x, "/api/ApiFileCategories"); 

Generic Class here. You can pass any type

 public class SynchronousPost<T>where T :class
    {
        public SynchronousPost()
        {
            Client = new WebClient { UseDefaultCredentials = true };
        }

        public void PostEntity(T PostThis,string ApiControllerName)//The ApiController name should be "/api/MyName/"
        {
            //this just determines the root url. 
            Client.BaseAddress = string.Format(
         (
            System.Web.HttpContext.Current.Request.Url.Port != 80) ? "{0}://{1}:{2}" : "{0}://{1}",
            System.Web.HttpContext.Current.Request.Url.Scheme,
            System.Web.HttpContext.Current.Request.Url.Host,
            System.Web.HttpContext.Current.Request.Url.Port
           );
            Client.Headers.Add(HttpRequestHeader.ContentType, "application/json;charset=utf-8");
            Client.UploadData(
                                 ApiControllerName, "Post", 
                                 Encoding.UTF8.GetBytes
                                 (
                                    JsonConvert.SerializeObject(PostThis)
                                 )
                             );  
        }
        private WebClient Client  { get; set; }
    }

My Api classs looks like this, if you are curious

public class ApiFileCategoriesController : ApiBaseController
{
    public ApiFileCategoriesController(IMshIntranetUnitOfWork unitOfWork)
    {
        UnitOfWork = unitOfWork;
    }

    public IEnumerable<FileCategory> GetFiles()
    {
        return UnitOfWork.FileCategories.GetAll().OrderBy(x=>x.CategoryName);
    }
    public FileCategory GetFile(int id)
    {
        return UnitOfWork.FileCategories.GetById(id);
    }
    //Post api/ApileFileCategories

    public HttpResponseMessage Post(FileCategory fileCategory)
    {
        UnitOfWork.FileCategories.Add(fileCategory);
        UnitOfWork.Commit(); 
        return new HttpResponseMessage();
    }
}

I am using ninject, and repo pattern with unit of work. Anyways, the generic class above really helps.

hidden
  • 3,216
  • 8
  • 47
  • 69
1

Set identity's impersonation to true and validateIntegratedModeConfiguration to false in web.config

<configuration>
  <system.web>
    <authentication mode="Windows" />
    <authorization>
      <deny users="?" />
    </authorization>
    <identity impersonate="true"/>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" ></validation>
  </system.webServer>
</configuration>
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
metkelb
  • 11
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 06 '22 at 01:44
-2
string url = "https://www..com";
System.Windows.Forms.WebBrowser webBrowser = new System.Windows.Forms.WebBrowser();
this.Controls.Add(webBrowser);

webBrowser.ScriptErrorsSuppressed = true;
webBrowser.Navigate(new Uri(url));

var webRequest = WebRequest.Create(url);
webRequest.Headers["Authorization"] = "Basic" + Convert.ToBase64String(Encoding.Default.GetBytes(Program.username + ";" + Program.password));
          
webRequest.Method = "POST";
        
adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • Hello, please see https://meta.stackoverflow.com/editing-help Thanks! – Eric Aya Oct 26 '22 at 11:54
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 29 '22 at 00:27