Entity Framework Core 2 access token in DBcontext

Viewed 591

I am trying to access the current user in the DB context using IHttpContextAccessor. I am using JWT tokens to authenticate.

However the user is always null even when I am logged in. I can access the user details from the controller with no problem.

    private readonly IHttpContextAccessor _context;
    public ClientResolverService(IHttpContextAccessor context)
    {
        _context = context;
    }

    public string GetUser()
    {

        return _context.HttpContext.User?.Identity?.Name;
    }    
    return _context.HttpContext.User?.Identity?.Name;

I have added

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

to the startup.cs

Is it possible to access the token in the DBContext ?

0 Answers
Related