23

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

tmg
  • 19,895
  • 5
  • 72
  • 76
Nazmul Hossain
  • 2,085
  • 5
  • 25
  • 34

2 Answers2

51

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.

tmg
  • 19,895
  • 5
  • 72
  • 76
  • As so often with opaque wizards like identity framework 2.0, I wouldn't say I fully understand this, but it works like a dream. Found it helpful to use with this blog on separating the user name and email address in MVC identity framework 2: http://marcinjuraszek.com/2014/03/asp-net-identity-2-0-0-username-and-email-separation.html – Andy Brown May 24 '16 at 07:41
  • This pass word is encrypted can you use that as it is on PasswordSignInAsync mehtod? – Gayan Oct 14 '16 at 06:03
  • @GayanRanasinghe PasswordHash is stored in database. In these methods the password argument is the original password – tmg Oct 14 '16 at 06:12
  • ahh my bad i just noted that you never used stored password sorry – Gayan Oct 14 '16 at 06:14
  • 1
    What if there are multiple users that have same email adress? If you make email field unique then you must confirm the email adress before registering because X person can use Y person's email address without confirmation. This leads Y person not being able to register since his email adress is registered by X. – ninbit Oct 16 '19 at 16:07
3

We wanted to be able to allow our users to login via Username OR Email. This is how we set it up:

var isEmail = ValidationManager.IsValidEmailAddress(model.Username);

SignInStatus result = SignInStatus.Failure;

if (!isEmail)
   {
       result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, false);
   }
   else
   {
       var user = await UserManager.FindByEmailAsync(model.Username);
       if (user != null)
           result = await SignInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, false);
   }

IsValidEmailAddress is a customized variant of the property decoration [EmailAddress] validation attribute, we didn't use the built in one because it doesn't impose a length check on the TLD and allows email addresses like x@x.c

public static bool IsValidEmailAddress(string email)
    {
        if (string.IsNullOrEmpty(email))
            return false;

        var pattern = GlobalConstants.EMAIL_VALIDATION_PATTERN;
        const RegexOptions options = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture;

        var emailValidator = new Regex(pattern,options);

        return emailValidator.IsMatch(email);
    }

The RegEx pattern:

public const string EMAIL_VALIDATION_PATTERN = @"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))){2,63}\.?$";