/// <summary> /// Инициализирует базу данных /// </summary> /// <param name="users">репозиторий пользователей</param> /// <param name="roles">репозиторий ролей</param> /// <param name="provider">сервис вычислений хэша</param> /// <param name="logger">логгер</param> public static void Seed(IRepository <User> users, IRepository <Role> roles, IHashingProvider provider, ILogger logger) { logger.LogInformation("Database seeder started"); if (!(users.GetAll().Any() || roles.GetAll().Any())) { using (var hasher = System.Security.Cryptography.SHA1.Create()) { User Admin = new User { FirstName = "Администратор", SecondName = "Администратор", ThirdName = "Администратор", Username = "******", PasswordHash = provider.Hash("admin") }; Role administrator = new Role { Name = "Администратор" }; Role user = new Role { Name = "Пользователь" }; Admin.Roles.Add(administrator); Admin.Roles.Add(user); administrator.Users.Add(Admin); user.Users.Add(Admin); users.Add(Admin).Wait(); } logger.LogInformation("Database seeded"); } else { logger.LogInformation("Database is already seeded. Skipping this part"); } }
public async Task <bool> ValidateAsync(Guid userId, string password, Guid executedByUserId) { using (var connection = new SqlConnection(_settings.ConnectionString)) { var parameters = new DynamicParameters(); parameters.Add("userName", userId); parameters.Add("password", _hashingProvider.Hash(password, true)); parameters.Add("executedByUserId", executedByUserId); parameters.Add("found", dbType: DbType.Boolean, direction: ParameterDirection.Output); await connection.OpenAsync(); await connection.QueryAsync( "[dbo].[VerifyPassword]", parameters, commandType : CommandType.StoredProcedure, commandTimeout : (int)_settings.QueryTimeout.TotalSeconds); var found = parameters.Get <bool>("found"); return(found); } }
public async Task ValidateAsync_WhenCredentialsValid_Returns_True() { var userDto = new UserDto { Username = Guid.NewGuid().ToString("N") }; var password = _hashingProvider.Hash("Hello123!!", true); try { await AddUserAsync(userDto); var addedUserDto = await _userRepository.GetUserAsync(userDto.Username, _userId); await AddCredentialAsync(addedUserDto.Id, password); var isValid = await _sqlCredentialValidator.ValidateAsync(addedUserDto.Id, password, _userId); isValid.Should().BeTrue(); } finally { } }
public TValue Get(TKey key) { var hashKey = HashProvider.Hash(key); return(Get(hashKey)); }
public bool GreaterThan(TKey key) { var hashKey = HashProvider.Hash(key); return(HashKey > hashKey); }