Пример #1
0
        public async Task <IActionResult> TwoFactorAuthentication()
        {
            var user = await this.userManager.GetUserAsync(User);

            var model = new AdminTwoFactorAuthenticationViewModel
            {
                HasAuthenticator = await this.userManager.GetAuthenticatorKeyAsync(user) != null,
                Is2faEnabled     = user.TwoFactorEnabled
            };

            await LoadSharedKeyAndQrCodeUriAsync(user, model);

            return(View(model));
        }
Пример #2
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(User user, AdminTwoFactorAuthenticationViewModel model)
        {
            var unformattedKey = await this.userManager.GetAuthenticatorKeyAsync(user);

            if (string.IsNullOrEmpty(unformattedKey))
            {
                await this.userManager.ResetAuthenticatorKeyAsync(user);

                unformattedKey = await this.userManager.GetAuthenticatorKeyAsync(user);
            }

            model.SharedKey        = FormatKey(unformattedKey);
            model.AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey);
        }
Пример #3
0
        public async Task <IActionResult> TwoFactorAuthentication()
        {
            var user = await this.CurrentUserProvider.GetCurrentUserAsync();

            var model = new AdminTwoFactorAuthenticationViewModel
            {
                HasAuthenticator = await this.twoFactorAuthenticationService.GetFormattedKeyAsync(user) != null,
                Is2faEnabled     = this.twoFactorAuthenticationService.IsTwoFactorEnabled(user),
            };

            await this.LoadSharedKeyAndQrCodeUriAsync(user, model);

            return(this.View(model));
        }
Пример #4
0
        public async Task <IActionResult> TwoFactorAuthentication(AdminTwoFactorAuthenticationViewModel model)
        {
            try
            {
                var user = await this.CurrentUserProvider.GetCurrentUserAsync();

                var requestResult = await this.Mediator.Send(new ActivateTwoFactorAuthenticationCommand(model.Code));

                if (requestResult.Succeeded)
                {
                    var responseModel = new AdminTwoFactorAuthenticationViewModel
                    {
                        HasAuthenticator = await this.twoFactorAuthenticationService.GetFormattedKeyAsync(user) != null,
                        Is2faEnabled     = this.twoFactorAuthenticationService.IsTwoFactorEnabled(user),
                    };

                    this.ShowSuccessNotification("Two Factor Authenticator has been enabled successfully.");

                    return(this.View(responseModel));
                }
                else
                {
                    this.ModelState.AddModelError("Code", "Verification code is invalid.");
                    await this.LoadSharedKeyAndQrCodeUriAsync(user, model);
                }
            }
            catch (ValidationException ex)
            {
                this.ModelState.ApplyValidationException(ex, true);
            }
            catch (Exception)
            {
                this.ModelState.AddModelError(string.Empty, "Two factor authentication operation has failed.");
            }

            return(this.View(model));
        }
Пример #5
0
        public async Task <IActionResult> TwoFactorAuthentication(AdminTwoFactorAuthenticationViewModel model)
        {
            var user = await this.userManager.GetUserAsync(User);

            if (!ModelState.IsValid)
            {
                await LoadSharedKeyAndQrCodeUriAsync(user, model);

                return(View(model));
            }

            var verificationCode = model.Code.Replace(" ", string.Empty).Replace("-", string.Empty);

            var is2faTokenValid = await this.userManager.VerifyTwoFactorTokenAsync(
                user, this.userManager.Options.Tokens.AuthenticatorTokenProvider, verificationCode);

            if (!is2faTokenValid)
            {
                ModelState.AddModelError("Code", "Verification code is invalid.");
                await LoadSharedKeyAndQrCodeUriAsync(user, model);

                return(View(model));
            }

            await this.userManager.SetTwoFactorEnabledAsync(user, true);

            var responseModel = new AdminTwoFactorAuthenticationViewModel
            {
                HasAuthenticator = await this.userManager.GetAuthenticatorKeyAsync(user) != null,
                Is2faEnabled     = user.TwoFactorEnabled
            };

            TempData["SuccessStatusMessage"] = "Two Factor Authenticator has been enabled successfully.";

            return(View(responseModel));
        }
Пример #6
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(IUser user, AdminTwoFactorAuthenticationViewModel model)
        {
            model.SharedKey = await this.twoFactorAuthenticationService.GetFormattedKeyAsync(user);

            model.AuthenticatorUri = await this.twoFactorAuthenticationService.GenerateQrCodeUriAsync(user);
        }