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

前端 未结 2 1891
春和景丽
春和景丽 2021-01-26 18:03

I have an angular project and i am using .net core 2.o Web API. I stored my user info in Jwt and i want log every database operation. I can access user info by sending jwt and t

2条回答
  •  渐次进展
    2021-01-26 18:42

    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> GetAllStudents()
        {
            var requestedUserId= _httpContextAccessor.HttpContext.Headers["Authorization"];
            LogOperation(requestedUserId);
            return context.Students.ToList();
        }
    }
    

提交回复
热议问题