Created
July 31, 2014 10:51
-
-
Save alexdresko/ce6e8ac478fca31d36c0 to your computer and use it in GitHub Desktop.
Identity v2 Login with email confirmation and 2FA support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var user = await UserManager.FindByNameAsync(model.UserName); | |
if (user != null) | |
{ | |
if (!await UserManager.IsEmailConfirmedAsync(user.Id)) return View(Mvc.Account.Views.ErrorNotConfirmed); | |
} | |
//var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: true); | |
var result = await SignInManager.SignInAsync(model.UserName, model.Password, model.RememberMe); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ApplicationSignInManager : SignInManager<ApplicationUser, string> | |
{ | |
public ApplicationSignInManager(UserManager<ApplicationUser, string> userManager, IAuthenticationManager authenticationManager) | |
: base(userManager, authenticationManager) | |
{ | |
} | |
public async Task<SignInStatus> SignInAsync(string userName, string password, bool rememberMe) | |
{ | |
var user = await UserManager.FindByNameAsync(userName); | |
if (user == null) return SignInStatus.Failure; | |
if (await UserManager.IsLockedOutAsync(user.Id)) return SignInStatus.LockedOut; | |
if (!await UserManager.CheckPasswordAsync(user, password)) | |
{ | |
await UserManager.AccessFailedAsync(user.Id); | |
if (await UserManager.IsLockedOutAsync(user.Id)) | |
{ | |
return SignInStatus.LockedOut; | |
} | |
return SignInStatus.Failure; | |
} | |
if (await UserManager.GetTwoFactorEnabledAsync(user.Id)) | |
{ | |
if (!(await UserManager.IsEmailConfirmedAsync(user.Id) && await UserManager.IsPhoneNumberConfirmedAsync(user.Id))) | |
{ | |
return SignInStatus.RequiresVerification; | |
} | |
} | |
await base.SignInAsync(user, rememberMe, false); | |
return SignInStatus.Success; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment