3

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?

user2370664
  • 381
  • 5
  • 8
  • 30
  • Can you show us your `program.cs` file because I didn't reproduce your problem. – LouraQ Sep 30 '20 at 08:19
  • I added the Program.cs in post above – user2370664 Sep 30 '20 at 12:56
  • I used the same code as you, but the test can get the name, so I suggest you create a new project to test it again. – LouraQ Oct 02 '20 at 07:14
  • You are correct. This was caused because i needed to use a custom ClaimsTransformer to allow access based on Active Directory groups. The user name was not being copied to the new ClaimsPrincipal return identity. – user2370664 Oct 20 '20 at 10:14

2 Answers2

2

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
Patrick
  • 1,635
  • 2
  • 13
  • 23
  • Any idea why I can't see such options under properties of my web app project (Blazor, .NET 5)? – adam.k Jun 07 '21 at 09:02
1

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

Mubarak
  • 31
  • 5