예제 #1
0
        public async Task <IActionResult> EnableAuthenticator(EnableAuthenticatorViewModel ViewModel)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound());
            }
            if (!ModelState.IsValid)
            {
                return(View(await LoadSharedKeyAndQrCodeUriAsync(user)));
            }

            var VerificationCode = ViewModel.Code.Replace(" ", string.Empty).Replace("-", string.Empty);
            var is2faTokenValid  = await _userManager.VerifyTwoFactorTokenAsync(user, _userManager.Options.Tokens.AuthenticatorTokenProvider, ViewModel.Code);

            if (!is2faTokenValid)
            {
                ModelState.AddModelError(string.Empty, "کد اعتبارسنجی نامعتبر است.");
                return(View(await LoadSharedKeyAndQrCodeUriAsync(user)));
            }

            await _userManager.SetTwoFactorEnabledAsync(user, true);

            if (await _userManager.CountRecoveryCodesAsync(user) == 0)
            {
                var RecoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);

                ViewBag.Alert = "اپلیکشن احراز هویت شما تایید شده است";
                return(View("ShowRecoveryCodes", RecoveryCodes));
            }

            else
            {
                return(RedirectToAction("TwoFactorAuthentication", new { alert = "success" }));
            }
        }