How to use UserManager and SignInManager in Asp.net

UserManager and SignInManager in Asp.Net Core Identity

Asp.net core provides two important services.

  1. UserManager
  2. SignInManager

The above 2 services are used to create users, validate the user and then add it to the database.

These are 2 core services of Identity framework. To use these services we need to inject these services in the controller and we inject this in the constructor like below.

public class AccountController : Controller
    {
        private ILogger logger;
        private readonly UserManager<IdentityUser> _user;
        private readonly SignInManager<IdentityUser> _signInManager;

        public AccountController(ILogger<AccountController> logger,UserManager<IdentityUser> user,SignInManager<IdentityUser> signInManager)
        {
            this.logger = logger;
            _user = user;
            _signInManager = signInManager;
        }
    }

UserManager:

This user manager took one generic parameter which is IdentityUser. UserManager has following methods

  1. CreateAsync
  2. DeleteAsync
  3. UpdateAsync
  4. Etc…

SignInManager:

This also takes a generic parameter which is IdentityUser. This has below methods.

  1. SignInAsync
  2. SignOutAsync
  3. IsSignIn
  4. Etc…

Now see the Register method

  [HttpPost]
        public async Task<IActionResult> Register(RegistrationVIewModel model)
        {
            if (ModelState.IsValid)
            {
                var user=new IdentityUser()
                {
                    UserName = model.Email,
                    Email = model.Email
                };
             var result= await  _user.CreateAsync(user, model.Password);

             if (result.Succeeded)
             {
                 _signInManager.SignInAsync(user, true);
             }
            }
            
            return View();
            
        }

In this, we first create object of IdentityUser and assign the value to parameter from model to that user. After this we use the CreateAsync method of UserManager class and call them to create a user.

Our createUser is an async method so we will use the await keyword and store result in variable.If we are created successfully then we use the signInManager method to signIn which takes 2 parameters of one of the overloaded methods. In this first parameter is user and second parameter is Boolean flag for cookies. 2 type of cookies

  1. Session Cookie:- when the browser is closed the cookie will remove or deleted.
  2. Permanent Cookie:- Even when the browser is closed the cookie is stored on client machine.

If some error occurred we need to add this model state to achieve this we do following

foreach (var errors in result.Errors)
             {
                 ModelState.AddModelError("",errors.Description);
             }

Then this will display on our view.

In the above fig, you will able to see that when I used weak password the errors are propagated to this and it will display on view.

After this if you fill information correctly then it will create the user and add it to the database table which is AspNetUsers tables and password is stored in hash format means even if anyone connects to this database it will not see password easily.

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Related posts