Beispiel #1
0
        public async Task <IActionResult> EnableAuthenticator(AuthenicatorModel model)
        {
            if (model == null)
            {
                throw new Exception("Model is null");
            }

            if (!this.ModelState.IsValid)
            {
                return(View(model));
            }

            if (string.IsNullOrEmpty(model.Code))
            {
                this.ModelState.AddModelError("model.Code", "Verification code not provided");
            }

            ConfaqueUser user = await this._userManager.GetUserAsync(this.User).ConfigureAwait(false);

            if (user == null)
            {
                throw new ApplicationException("User not found");
            }

            bool is2faTokenValid = await this._userManager.VerifyTwoFactorTokenAsync(
                user,
                this._userManager.Options.Tokens.AuthenticatorTokenProvider,
                model.Code).ConfigureAwait(false);

            if (!is2faTokenValid)
            {
                this.ModelState.AddModelError("model.Code", "Code is invalid");
            }

            IdentityResult result = await this._userManager.SetTwoFactorEnabledAsync(user, true).ConfigureAwait(false);

            if (!result.Succeeded)
            {
                this.ModelState.AddModelError("is2faEnabled", "Cannot Enable 2FA");
            }

            return(RedirectToAction("Index"));
        }
Beispiel #2
0
        public async Task <IActionResult> EnableAuthenticator()
        {
            ConfaqueUser user = await this._userManager.GetUserAsync(this.User).ConfigureAwait(false);

            string key = await this._userManager.GetAuthenticatorKeyAsync(user).ConfigureAwait(false);

            if (string.IsNullOrEmpty(key))
            {
                IdentityResult result = await this._userManager.ResetAuthenticatorKeyAsync(user).ConfigureAwait(false);

                if (result.Succeeded)
                {
                    key = await this._userManager.GetAuthenticatorKeyAsync(user).ConfigureAwait(false);
                }
            }

            AuthenicatorModel model = new AuthenicatorModel()
            {
                SharedKey        = this.FormatKey(key),
                AuthenticatorUri = this.GenerateQRCodeUri(user.Email, key)
            };

            return(View(model));
        }