在哪里可以将用户信息存储在.net核心API项目中(使用angular [英] where can be stored user info in .net core API project with angular
问题描述
我有一个Angular项目,我正在使用.net core 2.o Web API.我将用户信息存储在Jwt中,我想记录每个数据库操作.我可以通过发送jwt并从服务器端的request.header中获取用户信息.但是问题是,可以在哪里存储?在我以前的MVC项目中,我可以将其存储在会话中.但是这个项目是API.而且我们使用JWT而不是会话.我如何在请求开始到结束期间实现该存储UserInfo.我想从任何地方访问UserInfo.这是我的actionFilter:
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 taking from request.header in server side. But the problem is, where can be stored? In my old mvc projects, i could stored in session. But this project is API . And we work with JWT not session. How can i achieve that store UserInfo during the request start to end. And i want to access UserInfo from everywhere. This is my actionFilter:
public class TestFilterAttribute : System.Web.Http.Filters.FilterAttribute, IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
var requestedUserInfo= context.HttpContext.Request.Headers["Authorization"];
??????????????? = requestedUserInfo;
}
public void OnActionExecuted(ActionExecutedContext context)
{
}
}
我的体系结构是这样的:
Contoller =>服务=>存储库.
因此,我必须在所有方法中将参数作为UserInfo发送.因此,我不想将所有方法参数都添加为UserInfo.所以,我想学习摆脱这个问题.
My architecture is like that:
Contoller => Service => Repository.
So, i have to send parameters as UserInfo in all methods. So, i dont want add all methods parameter as UserInfo. So, i want to learn get rid of this problem.
MyController.cs
MyController.cs
[HttpGet("GetAllStudents")]
public async Task<ServiceResult>GetAllStudents()
{
var requestedUserId= context.HttpContext.Request.Headers["Authorization"];
return await (studentService.GetAllStudents(requestedUserId));
}
我的service.cs
My service.cs
public async Task<ServiceResult> GetAllStudents(int requestedUserId)
{
return await unitOfWork.studentRepo.GetAllStudents(requestedUserId);
}
我的repository.cs
My repository.cs
public async Task<List<Student>> GetAllStudents(int requestedUserId)
{
LogOperation(requestedUserId);
return context.Students.ToList();
}
您可以看到每个方法都发送requestedUserId.我该如何摆脱呢?
You can see that every method sending requestedUserId. How can i get rid of this?
推荐答案
我找到了解决方案.我们的用户信息已经存储在HttpContext中.我一直在寻找" HttpContextAccessor ".您可以通过依赖项注入进行注入,然后可以在任何地方使用它(例如dbcontext类或repo类)
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();
}
}
这篇关于在哪里可以将用户信息存储在.net核心API项目中(使用angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!