如何使用 Net Core 在 DbContext 中获取用户信息 [英] How to get user information in DbContext using Net Core
问题描述
我正在尝试开发一个类库,我想在其中实现自定义 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 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 中获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!