示例#1
0
        public async Task <UserTokenDto> CreateOrUpdate(CreateOrUpdateUserTokenDto createOrUpdateUserTokenDto)
        {
            const string query = @"
                UPDATE [UserTokens]
                SET [Value] = @Value
                WHERE [UserId] = @UserId 
                AND [LoginProvider] = @LoginProvider 
                AND [Name] = @Name;

                INSERT INTO [UserTokens] ([UserId], [LoginProvider], [Name], [Value])
                SELECT @UserId, @LoginProvider, @Name, @Value
                WHERE NOT EXISTS (SELECT [UserId] FROM [UserTokens] WHERE [UserId] = @UserId AND [LoginProvider] = @LoginProvider AND [Name] = @Name);
            ";

            using (var connection = _dbConnectionFactory.GetDbConnection())
            {
                var insertedRowsNum = await connection.ExecuteAsync(query, createOrUpdateUserTokenDto);

                if (insertedRowsNum == 0)
                {
                    return(null);
                }

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

                return(userTokenDto);
            }
        }
        public async Task SetAuthenticatorKeyAsync(ApplicationUser user, string key, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var createUserTokenDto = new CreateOrUpdateUserTokenDto
            {
                UserId        = user.Id,
                LoginProvider = ApplicationConstants.LoginProviderName,
                Name          = ApplicationConstants.TwoFA.AuthenticatorKeyTokenName,
                Value         = key
            };

            await _userTokenRepository.CreateOrUpdate(createUserTokenDto);
        }
        public async Task ReplaceCodesAsync(ApplicationUser user, IEnumerable <string> recoveryCodes, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var mergedCodes = string.Join(";", recoveryCodes);

            var createUserTokenDto = new CreateOrUpdateUserTokenDto
            {
                UserId        = user.Id,
                LoginProvider = ApplicationConstants.LoginProviderName,
                Name          = ApplicationConstants.TwoFA.RecoveryCodeTokenName,
                Value         = mergedCodes
            };

            await _userTokenRepository.CreateOrUpdate(createUserTokenDto);
        }