Login a user in ASP.NET Identity

Introduction


In this section, i will show you how to implement Authentication feature using ASP.NET Identity.

In order to login a user, we use the PasswordSignIn method.

PasswordSignIn


The PasswordSignIn method sign in a user using its username and password and returns the result of the operation as a SignInStatus enumeration.

SignInStatus result = SignInManager.PasswordSignIn(model.UserName, model.Password, model.RememberMe, shouldLockout: false);

The PasswordSignIn method is called by the SignInManager which is responsible for performing the sign-in operations.

The model.RememberMe value passed as a parameter of the method is used to enable the Remember me feature.

We will talk later on about the shouldLockout property.

SignInStatus


The SignInStatus is an enumeration that represents the possible outcomes from a sign in attempt.


Success The user sign in was successful
LockedOut The user is locked out
RequiresVerification The sign in operation requires addition verification (used in two factor authentication)
Failure The user sign in failed

SignInManager


We use the Get method to get the ApplicationSignInManager from the OWIN context.

ApplicationSignInManager SignInManager = HttpContext.GetOwinContext().Get<ApplicationSignInManager>();

The ApplicationSignInManager is defined in the IdentityConfig.cs file.

Namespace


In order to use the Get method, the PasswordSignIn method and the SignInStatus enum, you have to include the following namespace :

using Microsoft.AspNet.Identity.Owin;

Example


[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    ApplicationSignInManager SignInManager = HttpContext.GetOwinContext().Get<ApplicationSignInManager>();

    SignInStatus result = SignInManager.PasswordSignIn(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
            
    switch (result)
    {
        case SignInStatus.Success:
                return RedirectToAction("Index", "Home");

        case SignInStatus.LockedOut:
            return View("Lockout");

        case SignInStatus.Failure:
        default:
            ModelState.AddModelError("", "Invalid login attempt..");
            return View(model);
    }
}

In the above example, we get the information of the user to authenticate via an HTTP Post request. We get the sign-in manager from the OWIN context by using the Get method and then we use the PasswordSignIn method in order to sign in this user. We redirect the user to the appropriate view based on the result of the sign in operation. If the returned SignInStatus value is Failure, we add an error message to the ModelState by using the AddModelError method.

The LoginViewModel object is the ViewModel that we are using in order to render and validate the Login view.

public class LoginViewModel
{
    [Required]
    [Display(Name = "Username")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

The [ValidateAntiForgeryToken] attribute is used to prevent forgery of a request.