If you want to get a users username in a site that uses authentication then you can just use the User object and that will provide all claims associated with that user.

However if you want to get the username for a user where they’re not required to authenticate first then it’s a bit trickier. In order to get the Windows username of a user you need to update the launchSettings.json file to requets the Windows username by telling the browser Windows authentication is required and then it can be retrieved from the request context.

This obviously shouldn’t be used in an actual security context but can be handy to know in some situations.

launchSettings.json

First update the windowsAuthentication and anonymousAuthentication properties in your launchSettings.json file. Anonymous site access is still possible.

"iisSettings": {
	"windowsAuthentication": true,
	"anonymousAuthentication": false,
	"iisExpress": {
		"applicationUrl": "http://localhost:53321",
		"sslPort": 44320
	}
}

HomeController.cs

You can then get a users Windows username from a controller using the below code.

public IActionResult Index()
{
	string userName = HttpContext.User.Identity.Name;
	return View(userName);
}

Dependency Injection

If you want to access the Windows username in the Startup script or in repositories then you can dependency inject the HttpContextAccessor which will allow access to the User object. Further details can be found here.

 public virtual void ConfigureServices(IServiceCollection services)
{
	services.AddHttpContextAccessor();
	services.AddControllers();
}

5 Comments

Wei Cen · 30th October 2020 at 12:36 pm

It is very helpful, it resolves my problem. Thank you so much!

Antonio Giorgi · 9th March 2021 at 11:24 am

For my first app in .net core 5 I want to redirect into home page after windows authentication. In the past in my .net framework mvc application i used Response.Redirect(“/”) in Session_Start()
how can I do?
Thank’s. Tony

    Shinigami · 10th March 2021 at 10:20 am

    Hi,

    I usually use AAD authentication rather than windows authentication but I’m assuming the principles should be the same. After authenticating with AAD the user is redirected to a page where the returned token is validated and the the user is then redirected to the page they originally requested (https://docs.microsoft.com/en-us/azure/active-directory/develop/reply-url). I think it would be possible to update this signin-oidc page to always redirct the user to the home page rather than to whatever page they had originally requested.

Rajesh · 9th May 2021 at 7:39 am

How to I refer the returned value in my View? for example, you are returning userName and I want to show “Welcome” + userName. How do I achieve that?
Thanks!

    Shinigami · 10th May 2021 at 11:07 am

    You could just do return View(“Welcome ” + userName); from your controller.

Leave a Reply to Antonio Giorgi Cancel reply

Avatar placeholder

Your email address will not be published. Required fields are marked *