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
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();
}
}