Although the answer has already been accepted, I thought I would add another way. You do not need to register a user when you check their username and password combination, if they provided the correct data, all you need to store in temporary data is their username or their profile if you want, and then redirect them to the second page of the factor, which only after they provided the correct one-time password do you actually register the user.
This method avoids the need for additional attributes, which can be a pain for consistency.
This is an appropriate fragment on how to achieve it.
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] 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); } }
The following link contains all the information on how to implement this in ASP.NET MVC, the article is aimed at Google Authenticator, maybe this is not what you are working with, but the principle of user registration in etc. same; https://samjenkins.com/mvc-two-factor-authentication/
Satal source share