locked
Current User in ASP.NET Core is always null RRS feed

  • Question

  • User351619809 posted

    Hello All,

    I am trying to get the current user name who is logged in using windows authentication in .net controller and I am always getting null value in _userName on my localhost.

    Below is my code:

    public class AuthorizationController : ControllerBase
        {
       
    
    
            private readonly IHttpContextAccessor _httpContextAccessor;
    
            public AuthorizationController(IHttpContextAccessor httpContextAccessor)
            {
                _httpContextAccessor = httpContextAccessor;
                string _userName = httpContextAccessor.HttpContext.User.Identity.Name;
            }
    }
        
    Below is my appsettings.json file:
    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*",
      "iisSettings": {
        "windowsAuthentication": true,
        "anonymousAuthentication": false,
        "iisExpress": {
          "applicationUrl": "http://localhost:44314/"
        
        }
      }
    }
    
    any help will be highly appreciated.
    Tuesday, August 18, 2020 11:29 PM

All replies