同时安全地使用 ASP.NET Core 的 UserManager 和 IdentityDbContext
每当我执行与身份验证相关的任何操作时,我都会使用 UserManager
而不是 IdentityDbContext
。我总是避免同时使用两者,因为我不确定它们如何相互作用。
现在我发现自己处于需要查询数据库的情况,因此必须使用上下文。
我根据请求注册我的上下文,我认为在启动时调用 services.AddIdentityCore()
时 UserManager
也是以这种方式注册的。
我担心的是:如果我在一个中执行工作,那么从另一个中使用是否安全,是否会存在基于首先调用“保存”的隐式竞争条件,它们是否共享更改跟踪器等?
同时使用两者“安全”吗?
Whenever I do anything related to auth, I use UserManager
rather than IdentityDbContext
. I always avoided using both simultaneously as I'm unsure how they interact.
Now I find myself in a situation that I need to query the database, so must use the context.
I register my context as per request, I think UserManager
is also registered that way when calling services.AddIdentityCore()
in startup.
What I'm worried about: if I perform work in one then is it safe to use from the other, will there be implicit race conditions based on which calls "save" first, do they share a change tracker, etc.?
Is it "safe" to use both at the same time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 UserClaimsPrincipalFactory 的重写中使用两者,没有任何问题。我调用 UserManager.UpdateAsync() 来保存对用户的更新,并使用注入的上下文来检索与用户授权相关的其他数据。
I use both in an override of UserClaimsPrincipalFactory, without problems. I call UserManager.UpdateAsync() to save updates to the user, and use the injected context to retrieve additional data related to user authorizations.