1

I'm trying to access the User of the HttpContext via httpContextAccessor.HttpContext.User. Unfortunately, it is always null.

In my launchSettings.json I have windowsAuthentication set to true and anonymousAuthentication to false.

"iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iis": {
      "applicationUrl": "http://localhost/Sw.Intranet.Webclient",
      "sslPort": 0
    },
    "iisExpress": {
      "applicationUrl": "http://localhost:55425",
      "sslPort": 44340
    }
  }, 
[...]
}

In my Startup, I have the middleware configured in this order:

  • app.UseRouting()
  • app.UseAuthorization()
  • app.UseEndpoints(...)

I have configured the HttpContextAccessor: services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

Is there anything else I could check / I have missed why the User is always null?

Thanks in advance

xeraphim
  • 4,375
  • 9
  • 54
  • 102
  • What webserver are you using? IIRC only IIS (not IIS Express, not Kestrel) supports Windows Authentication. Also, try using Internet Explorer - I'm unsure to what extent Chrome (and Edge) support Windows Authentication. – Dai Mar 02 '21 at 09:13
  • Did you also add `app.UseAuthentication();` in the middleware? – Christian Held Mar 02 '21 at 10:03
  • Make sure you also install Websockets protocol on IIS as it can also cause httpContextAccessor to have null values. https://stackoverflow.com/questions/59538132/httpcontext-is-null-when-running-web-app-in-iis – Syed Rafay Sep 07 '22 at 05:25

0 Answers0