如何使用 Net Core 在 DbContext 中获取用户信息

编程入门 行业动态 更新时间:2024-10-17 19:21:31
本文介绍了如何使用 Net Core 在 DbContext 中获取用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试开发一个类库,我想在其中实现自定义 DbContext.在 DbContext 的 SaveChanges 方法中,我需要获取当前用户的信息(部门、用户名等)以进行审计.DbContext 代码的某些部分如下:

I am trying to develop a class library in which i want to implement custom DbContext. In the SaveChanges method of the DbContext, i need to get current user’s information(department, username etc.) for auditing purpose. Some part of the DbContext code is below:

public override int SaveChanges() { // find all changed entities which is ICreateAuditedEntity var addedAuditedEntities = ChangeTracker.Entries<ICreateAuditedEntity>() .Where(p => p.State == EntityState.Added) .Select(p => p.Entity); var now = DateTime.Now; foreach (var added in addedAuditedEntities) { added.CreatedAt = now; added.CreatedBy = ?; added.CreatedByDepartment = ? } return base.SaveChanges(); }

想到两个选项:

  • 使用 HttpContext.Items 来保存用户信息,注入 IHttpContextAccessor 并从HttpContext.Items(在这种情况下DbContext取决于HttpContext,是不是正确吗?)
  • 使用 ThreadStatic 对象而不是 HttpContext.Items 并从对象中获取信息(我阅读了一些帖子ThreadStatic 不安全)
  • Using HttpContext.Items to keep user information, injecting IHttpContextAccessor and getting information from the HttpContext.Items(In this case DbContext depends HttpContext, is it correct?)
  • Using ThreadStatic object instead of HttpContext.Items and getting information from the object( I read some posts that ThreadStatic is not safe)

问题:哪个最适合我的情况?您有其他建议吗?

Question : Which is the best fit into my case? Is there another way you suggest?

推荐答案

我实施了一种类似的方法,该方法在 这篇博文 主要涉及创建一个服务,该服务将使用依赖注入来注入 HttpContext(和潜在的用户信息)到特定的上下文中,或者您更喜欢使用它.

I implemented an approach similar to this that is covered in this blog post and basically involves creating a service that will use dependency injection to inject the HttpContext (and underlying user information) into a particular context, or however you would prefer to use it.

一个非常基本的实现可能看起来像这样:

A very basic implementation might look something like this:

public class UserResolverService { private readonly IHttpContextAccessor _context; public UserResolverService(IHttpContextAccessor context) { _context = context; } public string GetUser() { return _context.HttpContext.User?.Identity?.Name; } }

您只需要在 Startup.cs 文件中的 ConfigureServices 方法中将其注入管道:

You would just need to inject this into the pipeline within the ConfigureServices method in your Startup.cs file :

services.AddTransient<UserResolverService>();

最后,只需在您指定的 DbContext 的构造函数中访问它:

And then finally, just access it within the constructor of your specified DbContext :

public partial class ExampleContext : IExampleContext { private YourContext _context; private string _user; public ExampleContext(YourContext context, UserResolverService userService) { _context = context; _user = userService.GetUser(); } }

然后您应该能够使用 _user 在您的上下文中引用当前用户.这也可以轻松扩展为存储/访问当前请求中可用的任何内容.

Then you should be able to use _user to reference the current user within your context. This can easily be extended to store / access any content available within the current request as well.

更多推荐

如何使用 Net Core 在 DbContext 中获取用户信息

本文发布于:2023-11-13 22:35:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585509.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   用户信息   Core   Net   DbContext

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!