protected override void OnModelCreating(ModelBuilder builder) { #if DEBUG // create super admin account var hasher = new CryptoHash(new CryptoRandom()); var(hash, salt) = hasher.Pbkdf2Hash("T0p$ecret"); builder.Entity <User>().HasData(new User { Id = 1000, FirstName = "Super", LastName = "Admin", Email = "*****@*****.**", Role = UserRole.Admin | UserRole.BankOfficer, PasswordSalt = salt.GetBase64String(), PasswordHash = hash.GetBase64String() }); // create currencies builder.Entity <Currency>().HasData(new Currency[] { new Currency { Id = 1000, Name = "VND" }, new Currency { Id = 1001, Name = "USD" }, new Currency { Id = 1002, Name = "EUR" } }); #endif }
public string Login(string email, string password) { password = password.EnsureNotNullOrWhiteSpace(nameof(password)); var user = Get(email); var passwordHash = CryptoHash.Pbkdf2Hash(password, user.PasswordSalt.ParseBase64String()).GetBase64String(); if (passwordHash.IsOrdinalEqual(user.PasswordHash, true)) { return(JwtService.GenerateToken(user)); } else { throw new BusinessException("Email or password is not correct."); } }
public (int userId, int?bankAccountId) Create(string email, string password, string role, string firstName, string lastName, string bankAccount, string bankAccountCurrency) { password.EnsureNotNullOrWhiteSpace(nameof(password)); email.EnsureNotNullOrWhiteSpace(nameof(email)); firstName.EnsureNotNullOrWhiteSpace(nameof(firstName)); lastName.EnsureNotNullOrWhiteSpace(nameof(lastName)); var createBankAccount = !string.IsNullOrWhiteSpace(bankAccount) && !string.IsNullOrWhiteSpace(bankAccountCurrency); if (UserRepository.ByEmail(email) != null) { throw new BusinessException("Email is taken."); } if (createBankAccount && BankAccountService.Exists(bankAccount)) { throw new BusinessException("Bank account name is taken."); } if (!Enum.TryParse(role, out UserRole userRole)) { userRole = UserRole.Customer; } var(hash, salt) = CryptoHash.Pbkdf2Hash(password); var entity = new User { Email = email, Role = userRole, FirstName = firstName, LastName = lastName, PasswordSalt = salt.GetBase64String(), PasswordHash = hash.GetBase64String() }; UserRepository.Create(entity); UnitOfWork.SaveChanges(); int?bankAccountId = !createBankAccount ? (int?)null : BankAccountService.Create(bankAccount, bankAccountCurrency, entity.Id); return(entity.Id, bankAccountId); }