Esempio n. 1
0
        public async Task <IActionResult> TwoFactor(TwoFactorModel model)
        {
            var result = await HttpContext.AuthenticateAsync(IdentityConstants.TwoFactorUserIdScheme);

            if (!result.Succeeded)
            {
                ModelState.AddModelError("", "Your login request has expired, please start over.");
                return(View());
            }

            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByIdAsync(result.Principal.FindFirstValue("sub"));

                if (user != null)
                {
                    var isValid = await _userManager.VerifyTwoFactorTokenAsync(user,
                                                                               result.Principal.FindFirstValue("amr"), model.Token);

                    if (isValid)
                    {
                        await HttpContext.SignOutAsync(IdentityConstants.TwoFactorUserIdScheme);

                        var claimsPrincipal = await _claimsPrincipalFactory.CreateAsync(user);

                        var appProps = new AuthenticationProperties {
                            IsPersistent = true
                        };

                        await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme, claimsPrincipal, appProps);

                        var rememberMeProps = new AuthenticationProperties {
                            IsPersistent = true
                        };
                        var rememberMePrincipal = Store2FA(user.Id, _userManager.Options.Tokens.AuthenticatorTokenProvider, IdentityConstants.TwoFactorRememberMeScheme);
                        await HttpContext.SignInAsync(IdentityConstants.TwoFactorRememberMeScheme, rememberMePrincipal, rememberMeProps);

                        return(RedirectToAction("Index"));
                    }

                    ModelState.AddModelError("", "Invalid token");
                    return(View());
                }

                ModelState.AddModelError("", "Invalid Request");
            }

            return(View());
        }
Esempio n. 2
0
 public IActionResult TwoFactor(TwoFactorModel model)
 {
     return(View());
 }