.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)
{
}
}
我的架构是这样的:
控制器 => 服务 => 存储库.所以,我必须在所有方法中将参数作为 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);
}
我的仓库.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屋!