public UserController(IConfiguration configuration, EmailService emailService, PoliceComplainsContext context, DataProtectionService dataProtectionServiceService) { _configuration = configuration; _emailService = emailService; _dataProtectionService = dataProtectionServiceService; _context = context; }
public DashboardController(DataProtectionService dataProtectionService, PoliceComplainsContext context, IHostingEnvironment env, EmailService emailService, IConfiguration configuration) { _dataProtectionService = dataProtectionService; _configuration = configuration; _emailService = emailService; _context = context; _env = env; }
protected override void OnInitialized() { var status = DataProtectionService.Status(); if (status != ProtectionStatus.Activate) { NavigationManager.NavigateTo(""); } }
public void EncryptDecrypt_Fail(string text, string key) { // Arrange DataProtectionService service = new DataProtectionService(); // Act string encrypted = service.Encrypt(text, key); string decrypted = service.Decrypt(encrypted, key.Substring(1)); // Assert Assert.NotEmpty(encrypted); Assert.Empty(decrypted); Assert.NotEqual(text, encrypted); }
public async void Authenticate_Success() { // Arrange ApplicationDbContext dbContext = GetApplicationDbContext(_users[1]); IDataProtectionService dataProtectionService = new DataProtectionService(); IUserRepository userRepository = new UserRepository(dbContext, dataProtectionService); AccountService service = new AccountService(userRepository, dataProtectionService); // Act UserEntity user = await service.Authenticate(_users[1], CancellationToken.None); // Assert Assert.NotNull(user); Assert.NotNull(user.Password); }
public async void Authenticate_WrongUsername_Fail() { // Arrange ApplicationDbContext dbContext = GetApplicationDbContext(_users[1]); IDataProtectionService dataProtectionService = new DataProtectionService(); IUserRepository userRepository = new UserRepository(dbContext, dataProtectionService); AccountService service = new AccountService(userRepository, dataProtectionService); _users[1].Username = _users[1].Username.Substring(1); // Act UserEntity user = await service.Authenticate(_users[1], CancellationToken.None); // Assert Assert.Null(user); }
public async void Register_Success() { // Arrange ApplicationDbContext dbContext = GetApplicationDbContext(); IDataProtectionService dataProtectionService = new DataProtectionService(); IUserRepository userRepository = new UserRepository(dbContext, dataProtectionService); AccountService service = new AccountService(userRepository, dataProtectionService); // Act UserEntity user = await service.Register(_users[1], CancellationToken.None); // Assert Assert.NotNull(user); Assert.Null(user.Password); Assert.NotEqual(_users[1].Password, dbContext.Users.First().Password); }
public async void GetUsers_Success() { // Arrange ApplicationDbContext dbContext = GetApplicationDbContext(_users.ToArray()); IDataProtectionService dataProtectionService = new DataProtectionService(); IUserRepository userRepository = new UserRepository(dbContext, dataProtectionService); AccountService service = new AccountService(userRepository, dataProtectionService); // Act IEnumerable <UserEntity> users = await service.GetUsers(CancellationToken.None); // Assert Assert.NotNull(users); Assert.Equal(_users.Count, users.Count()); foreach (UserEntity user in users) { Assert.Null(user.Password); } }
private ApplicationDbContext GetApplicationDbContext(params UserEntity[] users) { ApplicationDbContext dbContext = ApplicationDbContextSqliteInMemory; if (users != null) { DataProtectionService dataProtectionService = new DataProtectionService(); foreach (UserEntity user in users) { UserEntity newUser = new UserEntity { Id = user.Id, Username = user.Username }; newUser.Password = dataProtectionService.HashPassword(user.Password); dbContext.Users.AddRange(newUser); } dbContext.SaveChanges(); } return(dbContext); }
private async Task ActivateAsync() { try { await ButtonSpinner.SpinAsync(async() => { var result = await DataProtectionService.ActivateProtectionAsync(Input.Password); if (!result) { ValidationErrorMessage.DisplayError(nameof(InputModel.Password), "Invalid password"); return; } NavigationManager.NavigateTo(""); }); } catch (Exception ex) { Logger.LogError(ex.Message); await ToastService.ShowToastAsync(ex.Message, ToastType.Error); } }
private async Task ChangeDataProtectionPasswordAsync() { try { await Button.SpinAsync(async() => { var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); await DataProtectionService.ChangeProtectionPasswordAsync(CurrentPassword.OldPassword, CurrentPassword.NewPassword); await ToastService.ShowToastAsync("Data protection password updated.", ToastType.Success); Logger.LogInformation($"Data protection password updated by {authState.User.Identity.Name}"); await ModalDialogClose(); }); } catch (Exception ex) { Logger.LogError(ex.Message); await ToastService.ShowToastAsync(ex.Message, ToastType.Error); } finally { await ModalDialogCancel(); } }
private async Task EnableDataProtectionAsync() { try { await ButtonSpinner.SpinAsync(async() => { var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); await DataProtectionService.EnableProtectionAsync(NewPassword.Password); await Refresh.InvokeAsync(this); await ToastService.ShowToastAsync("Data protection enabled.", ToastType.Success); await SynchronizationService.UpdateDataProtection(ExceptPageId); Logger.LogInformation($"Data protection enabled by {authState.User.Identity.Name}"); }); } catch (Exception ex) { Logger.LogError(ex.Message); await ToastService.ShowToastAsync(ex.Message, ToastType.Error); } finally { await ModalDialogService.CloseAsync(); } }
private void ProtectionStatus() { Status = DataProtectionService.Status(); StateHasChanged(); }