Is there a need for separate SignInManager<TUser> and UserManager<TUser>?
-
I have been looking at the code that gets generated when you add new scaffold items for Identity into an web application. I notice that often a UserManager<TUser> gets injected along with a SignInManager<TUser>. It seems that the SignInManager has a property for UserManager. Is there a reason to have them injected separately instead of just injecting and using the SignInManager? Can there be situations where the UserManager property is null in the SignInManager but would have been injected into the constructor?
// Should we always have a separate _userManager injected?
var userId = await _userManager.GetUserIdAsync(user);// Or would this also work, but some may not like the multiple chaining?
var userId = await _signInManager.UserManager.GetUserIdAsync(user); -
I have been looking at the code that gets generated when you add new scaffold items for Identity into an web application. I notice that often a UserManager<TUser> gets injected along with a SignInManager<TUser>. It seems that the SignInManager has a property for UserManager. Is there a reason to have them injected separately instead of just injecting and using the SignInManager? Can there be situations where the UserManager property is null in the SignInManager but would have been injected into the constructor?
// Should we always have a separate _userManager injected?
var userId = await _userManager.GetUserIdAsync(user);// Or would this also work, but some may not like the multiple chaining?
var userId = await _signInManager.UserManager.GetUserIdAsync(user);It comes down to separation of concerns. SignInManager is used by the UI to get credentials, authenticate, and maintain login state, while UserManager is concerned with managing the database of users and authenticating credentials passed to it by a SignInManager. Yes, the SignInManager has a UserManager property, but you have to pass a configured UserManager to the SignInManager when creating an instance of that.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
I have been looking at the code that gets generated when you add new scaffold items for Identity into an web application. I notice that often a UserManager<TUser> gets injected along with a SignInManager<TUser>. It seems that the SignInManager has a property for UserManager. Is there a reason to have them injected separately instead of just injecting and using the SignInManager? Can there be situations where the UserManager property is null in the SignInManager but would have been injected into the constructor?
// Should we always have a separate _userManager injected?
var userId = await _userManager.GetUserIdAsync(user);// Or would this also work, but some may not like the multiple chaining?
var userId = await _signInManager.UserManager.GetUserIdAsync(user);Yes, ASP.NET Identity requires distinct SignInManager and UserManager classes.
The management of user accounts, including their creation, updating, and deletion, password resets, and email address confirmations, is under the purview of the UserManager class
SignInManager class is responsible for handling the authentication and sign-in process for the application