- Home
- ASP.NET Core
- Identity
- Get the currently logged-in user's identifier
Get the currently logged-in user's identifier with ASP.NET Core Identity
To get some data about the currently logged-in user, you have to call the Microsoft.AspNetCore.Identity.UserManager<T> service, which implements all the methods you need.
A good practice is to add a private method in your controller, calling this service.
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
public AccountController(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
[HttpGet]
public async Task<string> GetCurrentUserId()
{
ApplicationUser usr = await GetCurrentUserAsync();
return usr?.Id;
}
private Task<ApplicationUser> GetCurrentUserAsync() => _userManager.GetUserAsync(HttpContext.User);
}
07/05/2016

