public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { WebSecurity.CreateUserAndAccount(model.UserName, model.Password); WebSecurity.Login(model.UserName, model.Password); // Create the two factor secret key var profile = MvcTFAProfile.GetProfile(model.UserName); profile.SecretKey = Base32Encoder.ToBase32String(GoogleAuthenticator.GenerateSecretKey()); return(RedirectToAction("Index", "Home")); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult UserProfile() { var model = new UserProfileModel(); var profile = MvcTFAProfile.GetCurrent(); // We need this to generate the QR code model.AppName = ConfigurationManager.AppSettings["AppName"]; model.UsesTwoFactor = profile.UsesTwoFactorAuthentication; model.SecretKey = profile.SecretKey; return(View(model)); }
public ActionResult UserProfile(UserProfileModel model) { var profile = MvcTFAProfile.GetCurrent(); if (ModelState.IsValid) { profile.UsesTwoFactorAuthentication = model.UsesTwoFactor; } // Make sure to include the secret key otherwise it can't be used for generating // the QR code. model.SecretKey = profile.SecretKey; model.AppName = ConfigurationManager.AppSettings["AppName"]; return(View(model)); }
public ActionResult Login(LoginModel model, string returnUrl) { if (ModelState.IsValid) { if (Membership.ValidateUser(model.UserName, model.Password)) { var profile = MvcTFAProfile.GetProfile(model.UserName); if (profile.UsesTwoFactorAuthentication) { TempData[CurrentUserTempDataKey] = profile; TempData[RememberMeTempDataKey] = model.RememberMe; return(RedirectToAction("SecondFactor", new { returnUrl = returnUrl })); } FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); return(RedirectToLocal(returnUrl)); } } // If we got this far, something failed, redisplay form ModelState.AddModelError("", "The user name or password provided is incorrect."); return(View(model)); }