public static Account Authenticate(string username, string password, IHashFactory hashFactory) { using (JamServerEntities dbContext = new JamServerEntities()) { Account account = dbContext.Accounts.SingleOrDefault(x => x.Username == username); if (account != null) { AccountAccessCode accessCode = dbContext.AccountAccessCodes.SingleOrDefault(x => x.AccountID == account.AccountID); if (accessCode != null && hashFactory.ValidateString(password, accessCode.AccessCode)) { account.AccountAccessCodes.Clear(); return(account); } else { throw new InvalidAccessCodeException(); } } else { throw new InvalidUsernameException(); } } }
public static Account Generate(string username, string password, IHashFactory hashFactory, bool approved = false) { Account account = new Account() { AccountID = Guid.NewGuid(), LastUpdateUTC = DateTime.UtcNow, Username = username, Approved = approved }; AccountAccessCode accessCode = new AccountAccessCode() { AccountAccessCodeID = Guid.NewGuid(), LastUpdateUTC = DateTime.UtcNow, AccountID = account.AccountID, AccessCode = hashFactory.BuildHash(password) }; account.AccountAccessCodes.Add(accessCode); using (JamServerEntities dbContext = new JamServerEntities()) { dbContext.Accounts.Add(account); dbContext.SaveChanges(); } account.AccountAccessCodes.Clear(); return(account); }