How to login using email in identity 2?

Nazmul Hossain picture Nazmul Hossain · Dec 16, 2014 · Viewed 12.4k times · Source

In MVC5 Identity 2 SignInManager.PasswordSignInAsync take user name for login.

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

but my user name and email are not same. but i want to email address for login.So how can i do that? Thanks

Answer

tmg picture tmg · Dec 16, 2014

Get user from UserManager by email.

var user = UserManager.FindByEmail(email);

Then use SignInManager's PasswordSignInAsync with user's Username

var result = await SignInManager
.PasswordSignInAsync(user.UserName,password,isPersistent,shouldLockout);

Or inside your SignInManager add this method (ASP.NET Identity discussion)

public async Task<SignInStatus> PasswordEmailSignInAsync(string email, string password, bool isPersistent, bool shouldLockout)
{
      var user =  UserManager.FindByEmail(email);
      return await PasswordSignInAsync(user.UserName,password,isPersistent,shouldLockout);
}

Then use it same as PasswordSignInAsync but with user email instead of usermane.