public ActionResult Login(string returnUrl) { ViewBag.ReturnUrl = returnUrl; ViewBag.LiveDemoMemberships = Demo.GetLiveDemoMemberships(); ViewBag.EnableDemo = (ViewBag.LiveDemoMemberships != null && ViewBag.LiveDemoMemberships.Count > 0); ViewBag.EnableMobil = UserManager.TwoFactorProviders.ContainsKey("PhoneCode"); var initialModel = new LoginViewModel() { LoginState = ViewBag.EnableDemo ? LoginViewModel.State.Demo : LoginViewModel.State.Login }; var lastEmailLogin = Request.Cookies["LastEmailLogin"]; if (lastEmailLogin != null && EmailValidator.IsValid(lastEmailLogin.Value)) { initialModel.Email = lastEmailLogin.Value; } return View(initialModel); }
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (!EmailValidator.IsValid(model.Email)) { ModelState.AddModelError("Email", "Email kunne ikke valideres som værende korrekt email format."); } else { model.Email = EmailValidator.ParseEmail(model.Email); } if (!ModelState.IsValid) { ViewBag.ReturnUrl = returnUrl; ViewBag.LiveDemoMemberships = Demo.GetLiveDemoMemberships(); ViewBag.EnableDemo = (ViewBag.LiveDemoMemberships != null && ViewBag.LiveDemoMemberships.Count > 0); ViewBag.EnableMobil = UserManager.TwoFactorProviders.ContainsKey("PhoneCode"); model.LoginState = LoginViewModel.State.Login; return View(model); } // This doen't count login failures towards lockout only two factor authentication // To enable password failures to trigger lockout, change to shouldLockout: true var result = await SignInHelper.PasswordSignIn(model.Email, model.Password, model.RememberMe, shouldLockout: false); switch (result) { case SignInStatus.Success: var cookie = new HttpCookie("LastEmailLogin", model.Email); cookie.Expires = DateTime.Now.AddMonths(1); Response.AppendCookie(cookie); return RedirectToLocal(returnUrl); case SignInStatus.UnConfirmed: return RedirectToAction("EmailNotConfirmed", new { email = model.Email }); case SignInStatus.LockedOut: return View("Lockout"); case SignInStatus.RequiresTwoFactorAuthentication: return RedirectToAction("SendCode", new { ReturnUrl = returnUrl }); case SignInStatus.Failure: default: ViewBag.ReturnUrl = returnUrl; ViewBag.LiveDemoMemberships = Demo.GetLiveDemoMemberships(); ViewBag.EnableDemo = (ViewBag.LiveDemoMemberships != null && ViewBag.LiveDemoMemberships.Count > 0); ViewBag.EnableMobil = UserManager.TwoFactorProviders.ContainsKey("PhoneCode"); model.LoginState = LoginViewModel.State.Login; ModelState.AddModelError("", "Invalid login attempt."); return View(model); } }
public async Task<ActionResult> TokenLogin(LoginViewModel model, string returnUrl) { ViewBag.ReturnUrl = returnUrl; ViewBag.LiveDemoMemberships = Demo.GetLiveDemoMemberships(); ViewBag.EnableDemo = (ViewBag.LiveDemoMemberships != null && ViewBag.LiveDemoMemberships.Count > 0); ViewBag.EnableMobil = UserManager.TwoFactorProviders.ContainsKey("PhoneCode"); model.LoginState = LoginViewModel.State.TokenLogin; if (!model.MobilNumberValidated) { if (!MobilNumberValidator.IsValid(model.MobilNumber, true)) { ModelState.AddModelError("MobilNumber", "Der blev ikke fundet en pilot med dette nummer."); return View("Login", model); } else { model.MobilNumberValidated = true; model.MobilNumber = MobilNumberValidator.ParseMobilNumber(model.MobilNumber); var result = await SignInHelper.MobilSignIn(model.MobilNumber, model.RememberBrowser); switch (result) { case SignInStatus.LockedOut: return View("Lockout"); case SignInStatus.UnConfirmed: case SignInStatus.Success: case SignInStatus.RequiresTwoFactorAuthentication: model.MobilNumberValidated = true; // Does not function because we are working prior to cookies being written. //var userId = await SignInHelper.GetVerifiedUserIdAsync(); var user = await UserManager.FindByNameAsync(model.MobilNumber); var userId = user.Id; if (userId == null) { model.MobilNumberValidated = false; ModelState.AddModelError("MobilNumber", "Unable to find verified user"); return View("Login", model); } var userFactors = await UserManager.GetValidTwoFactorProvidersAsync(userId); if (userFactors.All(p => p != "PhoneCode")) { model.MobilNumberValidated = false; ModelState.AddModelError("MobilNumber", "SMS Provider not available"); return View("Login", model); } if (!await SignInHelper.SendTwoFactorCode("PhoneCode", userId)) { model.MobilNumberValidated = false; ModelState.AddModelError("MobilNumber", "Unable to send verification code"); return View("Login", model); } if (HttpContext.IsDebuggingEnabled) { // To exercise the flow without actually sending codes, uncomment the following line ModelState.AddModelError("VerifyCode", "For DEMO purposes the current verification code is: " + await UserManager.GenerateTwoFactorTokenAsync(userId, "PhoneCode")); } return View("Login", model); case SignInStatus.Failure: default: model.MobilNumberValidated = false; ModelState.AddModelError("MobilNumber", "Unable to sign-in"); return View("Login", model); } } } // Ready to handle Verification Code if (!string.IsNullOrWhiteSpace(model.VerifyCode)) { var result = await SignInHelper.TwoFactorSignIn("PhoneCode", model.VerifyCode, model.RememberBrowser, false); switch (result) { case SignInStatus.Success: return RedirectToLocal(returnUrl); case SignInStatus.UnConfirmed: return RedirectToAction("EmailNotConfirmed"); // State should not be possible to reach on mobil login accounts case SignInStatus.LockedOut: return View("Lockout"); case SignInStatus.Failure: default: ModelState.AddModelError("VerifyCode", "Invalid code"); return View("Login", model); } } return View("Login", model); }