.NET Core 3.1 User.Identity.Name always null with Windows Authentication

Viewed 8884

I am using Windows Authentication with a .net core 3.1 application. I would like to retrieve the current user name but it's always null. I have not been able to deploy this application to IIS yet so this is happening while debugging with IISExpress.

launchSettings.json:

{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,    
    "iisExpress": {
      "applicationUrl": "http://localhost:52362",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(IISDefaults.AuthenticationScheme);
            services.AddAuthorization();

            services.AddHttpContextAccessor();
            services.AddMvc();
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        }
        
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseAuthentication();
            app.UseRouting();
            app.UseAuthorization();
        }

Controller:

[Authorize]
[ApiController]
[Route("[controller]")]
public class FilingController : Controller
{
    private readonly string _currentUserName;
    public FilingController(IHttpContextAccessor accessor)
    {
        //This is always null
        _currentUserName = accessor.HttpContext.User.Identity.Name;
    }
}

Here is the Program.cs:

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.UseIISIntegration();
                    webBuilder.UseIIS();
                });
    }

This occurs after a successful login. Why is the Name null, how can this be fixed?

2 Answers

Looks like a possible duplicate of How does HttpContext.Current.User.Identity.Name know which usernames exist?

In Visual Studio Solution Explorer - Click on the web application project -> At the bottom select the Properties tab.Make sure you have the following set:

Anonymous Authentication | Disabled

Windows Authentication | Enabled

I already see in your code that you have set

"iisSettings": { "windowsAuthentication": true, "anonymousAuthentication": false,

However do the below validation steps to check if the Anonymous Authentication and Windows Authentication have been set according to your already configured iisSettings - if not, that is the issue

Validation Steps :

  1. select your project.
  2. Press F4
  3. Check if "Anonymous Authentication" is Disable and "Windows Authentication" is enable

When creating a new ASP.NET Core MVC application, make sure to select the Authentication Type "Windows" as in the picture below enter image description here

You can also update this feature if your application is created already, by going to the menu bar, select Project, select yourProject Properties, select Debug, check the "Enable Windows Authentication" checkbox, as in the image below

enter image description here

Related