1

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?

4
  • UserManager do query on db Commented Mar 10, 2022 at 11:39
  • I'm unsure how UserManager is registered -- i think you can get some clue from src services.TryAddScoped<UserManager<TUser>>(); Commented Mar 10, 2022 at 11:44
  • no you can't do it ..just the query it wrap in form of find by name etc .. but you can safely do your query with dbcontext on user table etc Commented Mar 10, 2022 at 11:45
  • No user manager doesn't expose the store. Commented Mar 10, 2022 at 11:46

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. So changes you make in one are visible in the other?
The changes are all saved to the database, so they are visible at the next data retrieval. I don't know if the in-use context is shared with the UserManager, but you could do a test to find out, if that's necessary in your scenario.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.