public async Task <IActionResult> Setup2FA()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(View());
            }

            var authkey = await _userManager.GetAuthenticatorKeyAsync(user);

            if (string.IsNullOrEmpty(authkey))
            {
                await _userManager.ResetAuthenticatorKeyAsync(user);

                authkey = await _userManager.GetAuthenticatorKeyAsync(user);
            }

            var model = new MfaCreateViewModel()
            {
                AuthKey = FormatAuthKey(authkey)
            };

            return(View(model));
        }
示例#2
0
        public async Task <IActionResult> Setup2Fa(MfaCreateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Errors in Page!");
                return(View(model));
            }
            var user = await _userManager.GetUserAsync(User);

            if (user == null || !user.EmailConfirmed)
            {
                ModelState.AddModelError(string.Empty, "User not found or User is not confirm.");
                return(View(model));
            }
            var isCodeCorrect = await _userManager.VerifyTwoFactorTokenAsync(user,
                                                                             _userManager.Options.Tokens.AuthenticatorTokenProvider, model.Code);

            if (!isCodeCorrect)
            {
                ModelState.AddModelError(string.Empty, "The code did not match they auth key! Please, try it again");
                return(View(model));
            }
            await _userManager.SetTwoFactorEnabledAsync(user, true);

            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> Setup2FA(MfaCreateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.GetUserAsync(User);

            var isCodeCorrect = await _userManager.VerifyTwoFactorTokenAsync(user, _userManager.Options.Tokens.AuthenticatorTokenProvider, model.Code);

            if (!isCodeCorrect)
            {
                ModelState.AddModelError("", "The code dod not match auth key!");
                return(View(model));
            }

            await _userManager.SetTwoFactorEnabledAsync(user, true);

            return(RedirectToAction("Index", "Logins"));
        }