问题描述
我正在尝试开发一个类库,我想在该类库中实现自定义 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 theHttpContext.Items
(In this caseDbContext
dependsHttpContext
, is itcorrect?) - Using ThreadStatic object instead of
HttpContext.Items
and getting information from the object( I read some poststhat 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中获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!