Example #1
0
        public async Task <RecoveryCodeOutputDto> EnableAuthenticator(EnableAuthenticatorInputDto model, bool isValid)
        {
            var user = await GetCurrentUserAsync();

            var result = new RecoveryCodeOutputDto();

            if (user == null)
            {
                result.IdentityResult = IdentityResult.Failed(new IdentityError[]
                {
                    new IdentityError()
                    {
                        Code        = "User",
                        Description = "Not Found User"
                    }
                });
            }

            if (!isValid)
            {
                await LoadSharedKeyAndQrCodeUriAsync(user, model);

                result.IdentityResult         = IdentityResult.Success;
                result.EnableAuthenticatorDto = model;
                return(result);
            }

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

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

            if (!is2faTokenValid)
            {
                result.IdentityResult = IdentityResult.Failed(new IdentityError[]
                {
                    new IdentityError()
                    {
                        Code        = "Code",
                        Description = "Verification code is invalid."
                    }
                });
                await LoadSharedKeyAndQrCodeUriAsync(user, model);

                result.EnableAuthenticatorDto = model;
                result.IdentityResult         = IdentityResult.Success;
                result.isView = true;
                return(result);
            }

            await _userManager.SetTwoFactorEnabledAsync(user, true);

            var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);

            result.RecoveryCodes = recoveryCodes.ToList();
            return(result);
        }
Example #2
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(User user, EnableAuthenticatorInputDto model)
        {
            var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);

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

                unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            }

            model.SharedKey        = FormatKey(unformattedKey);
            model.AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey);
        }
Example #3
0
        public async Task <IActionResult> EnableAuthenticator([FromBody] EnableAuthenticatorInputDto model)
        {
            var result = await _manageService.EnableAuthenticator(model, ModelState.IsValid);

            if (!result.IdentityResult.Succeeded)
            {
                if (result.isView)
                {
                    return(View(result.EnableAuthenticatorDto));
                }
                return(BadRequest(result.IdentityResult.Errors.Select(x => x.Description)));
            }
            return(Ok(result));
        }
Example #4
0
        public async Task <EnableAuthenticatorInputDto> EnableAuthenticator()
        {
            var user = await GetCurrentUserAsync();

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

            var model = new EnableAuthenticatorInputDto();

            await LoadSharedKeyAndQrCodeUriAsync(user, model);

            return(model);
        }