@manhng

Welcome to my blog!

Getting the current user in ASP.NET Core

April 9, 2021 14:30

Getting the current user in ASP.NET Core (edit)

https://www.koskila.net/how-to-get-current-user-in-asp-net-core/

How to Get the Current User in ASP.NET Core | TheCodeBuzz

c# - How to get current user in asp.net core - Stack Overflow

ASP.NET: Get Current User

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

ASP.NET Core: Get Current User

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

ASP.NET Core 3.0: Get Current User

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpContextAccessor();
}
private readonly IHttpContextAccessor _httpContextAccessor;
public ApplicationDbContext(DbContextOptions options, IHttpContextAccessor httpContextAccessor) : base(options) { _httpContextAccessor = httpContextAccessor; }

ASP.NET Core 3.0: Get Current User

using System.Security.Claims;

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

Categories

Recent posts