/// <summary> /// Creates a new account. /// </summary> /// <param name="email">E-Mail Address that the account should be registered with.</param> /// <param name="password">Password of the account.</param> /// <returns>A Task that will complete when the account has been created.</returns> public async Task CreateAccount(string email, string password) { // Check if email address is already used. if ((await GetAccountByEmail(email, suppressException: true)) != null) { throw new EmailAlreadyRegisteredException(email); } // If not, create new account. await _entityRepositoryFactory.Use(repository => { var hashedPassword = HashedPassword.CreateFromPlainPassword(password); var account = repository.Add(new Account() { EmailAddress = email, PasswordHash = hashedPassword.Hash, PasswordSalt = hashedPassword.Salt }); // Provide some starting budget for the new account. account.ContextFreeTransactions.Add(new ContextFreeTransaction() { Comment = "Initial", Amount = 10000, TransactionTimestamp = DateTime.UtcNow }); }); }
/// <summary> /// Changes the password of the specified account. /// </summary> /// <param name="email">E-Mail Address of the account of which to change the password.</param> /// <param name="newPassword">Password that should be used for the account.</param> /// <returns>A Task that will complete when the password has been changed.</returns> public async Task ChangePassword(int accountId, string newPassword) { var account = await _entityQuerySource.GetById <Account>(accountId); var hashedPassword = HashedPassword.CreateFromPlainPassword(newPassword); await _entityRepositoryFactory.Use(repository => { repository.Change <Account>(account.Id, a => { a.PasswordHash = hashedPassword.Hash; a.PasswordSalt = hashedPassword.Salt; }); }); }
public async Task AccountService_CheckPassword_Fail() { var repositoryMock = new EntityRepositoryMock(); var sut = new AccountService(repositoryMock, repositoryMock); var hashedPassword = HashedPassword.CreateFromPlainPassword("testpassword"); repositoryMock.Add(new Account { Id = 1, EmailAddress = "*****@*****.**", PasswordHash = hashedPassword.Hash, PasswordSalt = hashedPassword.Salt }); var result = await sut.CheckAccountPassword("*****@*****.**", "somethingelse"); Assert.AreEqual(false, result, "CheckPassword confirmed correctness."); }