Skip to content

Instantly share code, notes, and snippets.

@alexdresko
Created July 31, 2014 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdresko/ce6e8ac478fca31d36c0 to your computer and use it in GitHub Desktop.
Save alexdresko/ce6e8ac478fca31d36c0 to your computer and use it in GitHub Desktop.
Identity v2 Login with email confirmation and 2FA support
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);
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