.NET logo

How to get the current user in ASP.NET Core?

This post was most recently updated on May 20th, 2020.

Reading Time: 2 minutes.

This post describes how to get the currently logged in user using ASP.NET Core. This is compared with how it’s done in ASP.NET Framework.

Background

Table of Contents

In ASP.NET Core (and .NET Core in general), a lot of things are done a bit differently than what you might be used to if you’re coming from the .NET Framework world. That’s what I’ve been learning building more and more stuff with it, instead of good old .NET Framework. The learning curve is definitely there, but it’s not that bad – most of the things work in a similar fashion, but a huge number of details are different.

In ASP.NET Framework, you’d do this by accessing HttpContext.Current.User and its properties (see below for an example), in .NET Core this is handled via dependency injection.

var username = HttpContext.Current.User.Identity.Name;

You can’t do this in .NET Core. How to do it there, then?

Solution

This post describes how to manage user identity when developing your application – before that, you need to configure your application registration in a way that your identity provider actually supplies you with the user information in the first place. For Azure AD, see my other post, here:

When that all is done, let’s jump into the ways you can access User/Identity in your program!

First, you add this in your Startup.cs, in ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
     ...
 
     services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}

Or if you’re at least on .NET Core 3.0, you can do this as well:

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

Then you add these pieces of code to your Controller, database context or other class:

  1. Private variable to hold the injected reference to our HttpContextAccessor
  2. New variable with the correct type (or interface) to the class constructor (like IHttpContextAccessor httpContextAccessor below)
  3. Assignment from the injected variable (in the constructor) to the hidden private variable (to make the reference available outside the constructor)
private readonly IHttpContextAccessor _httpContextAccessor;
 
...
 
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IHttpContextAccessor httpContextAccessor)
            : base(options)
{
  _httpContextAccessor = httpContextAccessor;
}

For our little example call further below, you’ll need to add a reference to Claims like this:

using System.Security.Claims;

And finally, you call it like this – for example, to get the reference to the user, and grab their id:

var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;

This can be injected to Controllers and Views, Custom Components and if need be, properties passed to background Threads (don’t pass the context! That’ll break.)

If you’re using Razor pages, you can just do this:

@{
    var username = Context.User.Identity.Name;
 
    ...
}

Razor exposes HttpContext as Razor.Component.

And with this, you should be good! :)

mm
4.6 5 votes
Article Rating
Subscribe
Notify of
guest
47 Comments
Inline Feedbacks
View all comments
Mike Boutelle

it appears that in .NET Core 3.1 the functionality has been restored so that the answer becomes that var username = HttpContext.User.Identity.Name; does return the current user.

Srikanth Bollojula

helped me