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
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.
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 [email protected]
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}\.?$";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With