where can be stored user info in .net core API project with angular

帅比萌擦擦* 提交于 2019-12-02 12:38:24

you don't store it, Angular has to send the JWT in every request and Asp.Net Core webapi has to open it, validate it, and then read the user that made the request from it.

I found the solution. Our user info already stored in HttpContext. "HttpContextAccessor" is what i was looking for. You can inject by dependency injection and then you can use from everywhere (forexample dbcontext class or repo class)

public class StudentService : IStudentService
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public StudentService(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

public async Task<List<Student>> GetAllStudents()
    {
        var requestedUserId= _httpContextAccessor.HttpContext.Headers["Authorization"];
        LogOperation(requestedUserId);
        return context.Students.ToList();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!