同时安全地使用 ASP.NET Core 的 UserManager 和 IdentityDbContext

发布于 2025-01-13 11:03:18 字数 372 浏览 5 评论 0原文

每当我执行与身份验证相关的任何操作时,我都会使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿越时光隧道 2025-01-20 11:03:18

我在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文