2

I want to get current windows user name when user opens the website. My application uses Blazor server-side. To get current username, I added:

  1. In startup.cs:
    services.AddHttpContextAccessor(); (under ConfigureServices)
  2. In razor page:
    @inject IHttpContextAccessor httpContextAccessor
  3. In razor page method:
    string userName = httpContextAccessor.HttpContext.User.Identity.Name;

When I execute the code, the application throws an exception:

"An error has occurred. This application may no longer respond until reloaded."

I get this error only when I deploy on IIS. On local machine it works well.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
RKh
  • 13,818
  • 46
  • 152
  • 265

2 Answers2

1

I had a similar issue trying to access HttpContext for user information using Blazor. I found this here which was a life saver. HttpContext is NULL when running web app in IIS All I had to do to solve the problem was to enable WebSockets in IIS.

Here are the steps: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-3.1#iisiis-express-support

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 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 Jan 14 '22 at 23:49
0

If you were to change that string from

string userName = httpContextAccessor.HttpContext.User.Identity.Name

to

string userName = httpContextAccessor?.HttpContext?.User?.Identity?.Name??"Anonymous"

then it would probably work.

It's because your IIS settings are allowing anonymous access and so your Identity object is null.

You should check for the inner exceptions behind the application crash but that will be your issue. You need to prevent anonymous access on IIS to get the Windows authentication passed through.

To manage IIS refer here https://stackoverflow.com/a/5458963/12285843

Frank
  • 903
  • 7
  • 14
  • Yes, you are right. The modified you suggested worked but the user name is returning "Anonymous" and not the logged in user name. At present, in IIS I have enabled "Anonymous" authentication and "Windows" is disabled. Please advise what changes in IIS required and also what should be the Identity for Application Pool ? – RKh Nov 10 '21 at 13:14
  • That question is already answered on Stack Overflow. It is here https://stackoverflow.com/a/5458963/12285843 – Frank Nov 10 '21 at 13:15
  • Also note that App Pool identity is unrelated to Windows Auth user identity – Frank Nov 10 '21 at 13:16
  • I already checked that post. In Web.Config, since I am using ASP.NET Core, adding those lines is causing application to throw error – RKh Nov 10 '21 at 13:18
  • I have modified the web.config by adding . Disabled "Anonymous authentication" in IIS but now the application asking credentials, which is obvious but I am not able to login using the same credentials through which I logged into the server. – RKh Nov 10 '21 at 13:30
  • Did you enable Win Authentication? What kind of auth do you want ? – H H Nov 10 '21 at 14:09
  • @RKh All of this may be true, but it has nothing to do with your original question. Please accept this answer and mark as correct. You cannot just make authentication happen, you have to know what you are doing. – Frank Nov 10 '21 at 16:10