public override string GeneratePasswordResetToken(string userName, int tokenExpirationInMinutesFromNow) { if (string.IsNullOrEmpty(userName)) { throw CreateArgumentNullOrEmptyException("userName"); } using (Context context = new Context()) { dynamic user = context.Users.FirstOrDefault(Usr => Usr.UserName == userName); if (user == null) { throw new InvalidOperationException(string.Format("User not found: {0}", userName)); } if (!user.IsConfirmed) { throw new InvalidOperationException(string.Format("User not found: {0}", userName)); } string token = null; if (user.PasswordVerificationTokenExpirationDate > DateTime.UtcNow) { token = user.PasswordVerificationToken; } else { token = CodeFirstCrypto.GenerateToken(); } user.PasswordVerificationToken = token; user.PasswordVerificationTokenExpirationDate = DateTime.UtcNow.AddMinutes(tokenExpirationInMinutesFromNow); context.SaveChanges(); return(token); } }
public override bool ResetPasswordWithToken(string token, string newPassword) { if (string.IsNullOrEmpty(newPassword)) { throw CreateArgumentNullOrEmptyException("newPassword"); } using (Context context = new Context()) { dynamic user = context.Users.FirstOrDefault(Usr => Usr.PasswordVerificationToken == token && Usr.PasswordVerificationTokenExpirationDate > DateTime.UtcNow); if (user != null) { dynamic newhashedPassword = CodeFirstCrypto.HashPassword(newPassword); if (newhashedPassword.Length > 128) { throw new ArgumentException("Password too long"); } user.Password = newhashedPassword; user.PasswordChangedDate = DateTime.UtcNow; user.PasswordVerificationToken = null; user.PasswordVerificationTokenExpirationDate = null; context.SaveChanges(); return(true); } else { return(false); } } }
public override string ExtendedValidateUser(string userNameOrEmail, string password) { if (string.IsNullOrEmpty(userNameOrEmail)) { throw CreateArgumentNullOrEmptyException("userNameOrEmail"); } if (string.IsNullOrEmpty(password)) { throw CreateArgumentNullOrEmptyException("password"); } using (Context context = new Context()) { User user = null; user = context.Users.FirstOrDefault(Usr => Usr.UserName == userNameOrEmail); if (user == null) { user = context.Users.FirstOrDefault(Usr => Usr.EmailAddress == userNameOrEmail); } if (user == null) { return(string.Empty); } if (!user.IsConfirmed) { return(string.Empty); } dynamic hashedPassword = user.Password; bool verificationSucceeded = (hashedPassword != null && CodeFirstCrypto.VerifyHashedPassword(hashedPassword, password)); if (verificationSucceeded) { user.PasswordFailuresSinceLastSuccess = 0; } else { int failures = user.PasswordFailuresSinceLastSuccess; if (failures != -1) { user.PasswordFailuresSinceLastSuccess += 1; user.LastPasswordFailureDate = DateTime.UtcNow; } } context.SaveChanges(); if (verificationSucceeded) { return(user.UserName); } else { return(string.Empty); } } }
public override bool ChangePassword(string userName, string oldPassword, string newPassword) { if (string.IsNullOrEmpty(userName)) { throw CreateArgumentNullOrEmptyException("userName"); } if (string.IsNullOrEmpty(oldPassword)) { throw CreateArgumentNullOrEmptyException("oldPassword"); } if (string.IsNullOrEmpty(newPassword)) { throw CreateArgumentNullOrEmptyException("newPassword"); } using (Context context = new Context()) { dynamic user = context.Users.FirstOrDefault(Usr => Usr.UserName == userName); if (user == null) { return(false); } dynamic hashedPassword = user.Password; bool verificationSucceeded = (hashedPassword != null && CodeFirstCrypto.VerifyHashedPassword(hashedPassword, oldPassword)); if (verificationSucceeded) { user.PasswordFailuresSinceLastSuccess = 0; } else { int failures = user.PasswordFailuresSinceLastSuccess; if (failures != -1) { user.PasswordFailuresSinceLastSuccess += 1; user.LastPasswordFailureDate = DateTime.UtcNow; } context.SaveChanges(); return(false); } dynamic newhashedPassword = CodeFirstCrypto.HashPassword(newPassword); if (newhashedPassword.Length > 128) { throw new ArgumentException("Password too long"); } user.Password = newhashedPassword; user.PasswordChangedDate = DateTime.UtcNow; context.SaveChanges(); return(true); } }
public override string CreateAccount(string userName, string password, string email, string mobileNumber, string routingNumber, string accountNumber, int accountType, out MembershipCreateStatus createStatus, bool requireConfirmationToken) { if (string.IsNullOrEmpty(password)) { throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword); } string hashedPassword = CodeFirstCrypto.HashPassword(password); if (hashedPassword.Length > 128) { throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword); } if (string.IsNullOrEmpty(userName)) { throw new MembershipCreateUserException(MembershipCreateStatus.InvalidUserName); } if (string.IsNullOrEmpty(email)) { throw new MembershipCreateUserException(MembershipCreateStatus.InvalidEmail); } using (Context context = new Context()) { dynamic user = context.Users.FirstOrDefault(Usr => Usr.UserName == userName); dynamic emailuser = context.Users.FirstOrDefault(Usr => Usr.EmailAddress == email); if (user != null) { throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName); } if (emailuser != null) { throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateEmail); } string token = null; if (requireConfirmationToken) { token = CodeFirstCrypto.GenerateToken(); } int defaultNumPasswordFailures = 0; User NewUser = new User { MobileNumber = mobileNumber, UserId = Guid.NewGuid(), UserName = userName, Password = hashedPassword, SecurityPin = securityService.Encrypt("2589"), IsConfirmed = !requireConfirmationToken, EmailAddress = email, ConfirmationToken = token, CreateDate = DateTime.UtcNow, PasswordChangedDate = DateTime.UtcNow, PasswordFailuresSinceLastSuccess = defaultNumPasswordFailures, LastPasswordFailureDate = DateTime.UtcNow, PaymentAccounts = new Collection <PaymentAccount>() { new PaymentAccount() { AccountNumber = securityService.Encrypt(accountNumber), AccountType = (PaymentAccountType)accountType, NameOnAccount = securityService.Encrypt("James Rhodes"), RoutingNumber = securityService.Encrypt(routingNumber) } }, UserStatus = UserStatus.Pending, LastLoggedIn = System.DateTime.Now, Limit = 0 }; context.Users.Add(NewUser); context.SaveChanges(); createStatus = MembershipCreateStatus.Success; return(token); } }