This guy has a pretty good cheat sheet. Inside ConfigureServices in Startup.cs put:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
The rest of this code is Controller-flavored. Have a private field at a given Controller.
private readonly IHttpContextAccessor _httpContextAccessor;
At the constructor:
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,
IHttpContextAccessor httpContextAccessor) : base(options)
{
_httpContextAccessor = httpContextAccessor;
When you use it, loop in...
using System.Security.Claims;
...and then have:
var userId =
_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
No comments:
Post a Comment