public async Task <bool> RedeemCodeAsync(ApplicationUser user, string code, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var getUserTokenDto = new GetUserTokenDto
            {
                UserId        = user.Id,
                LoginProvider = ApplicationConstants.LoginProviderName,
                Name          = ApplicationConstants.TwoFA.RecoveryCodeTokenName
            };

            var recoveryCodes = await _userTokenRepository.Get(getUserTokenDto);

            var recoveryCodesSplitted = recoveryCodes.Value.Split(";");

            if (!recoveryCodesSplitted.Contains(code))
            {
                return(false);
            }

            var updatedRecoveryCodes = recoveryCodesSplitted.Where(t => t != code);

            await ReplaceCodesAsync(user, updatedRecoveryCodes, cancellationToken);

            return(true);
        }
Beispiel #2
0
        public async Task <UserTokenDto> Get(GetUserTokenDto getUserTokenDto)
        {
            const string query = @"
                SELECT *
                FROM [UserTokens]
                WHERE [UserId] = @UserId
                AND [LoginProvider] = @LoginProvider
                AND [Name] = @Name;
            ";

            using (var connection = _dbConnectionFactory.GetDbConnection())
            {
                var userToken = await connection.QuerySingleOrDefaultAsync(
                    query,
                    new
                {
                    @UserId        = getUserTokenDto.UserId,
                    @LoginProvider = getUserTokenDto.LoginProvider,
                    @Name          = getUserTokenDto.Name
                });

                var userTokenDto = _mapper.Map <UserTokenDto>(userToken);

                return(userTokenDto);
            }
        }
        public async Task <string> GetAuthenticatorKeyAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var getUserTokenDto = new GetUserTokenDto
            {
                UserId        = user.Id,
                LoginProvider = ApplicationConstants.LoginProviderName,
                Name          = ApplicationConstants.TwoFA.AuthenticatorKeyTokenName
            };

            var userTokenDto = await _userTokenRepository.Get(getUserTokenDto);

            return(userTokenDto?.Value);
        }
        public async Task <int> CountCodesAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var getUserTokenDto = new GetUserTokenDto
            {
                UserId        = user.Id,
                LoginProvider = ApplicationConstants.LoginProviderName,
                Name          = ApplicationConstants.TwoFA.RecoveryCodeTokenName
            };

            var recoveryCodes = await _userTokenRepository.Get(getUserTokenDto);

            var recoveryCodesSplitted = (recoveryCodes?.Value ?? string.Empty).Split(";");

            return(recoveryCodesSplitted.Length);
        }