public override string GeneratePasswordResetToken(string userName, int tokenExpirationInMinutesFromNow) { if (string.IsNullOrEmpty(userName)) { throw CreateArgumentNullOrEmptyException("userName"); } using ($safeprojectname$Context context = new $safeprojectname$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 string CreateAccount(string userName, string password, string email, 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 ($safeprojectname$Context context = new $safeprojectname$Context()) { dynamic user = context.Users.FirstOrDefault(Usr => Usr.Username == userName); dynamic emailuser = context.Users.FirstOrDefault(Usr => Usr.Email == 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 { UserId = Guid.NewGuid(), Username = userName, Password = hashedPassword, IsConfirmed = !requireConfirmationToken, Email = email, ConfirmationToken = token, CreateDate = DateTime.UtcNow, PasswordChangedDate = DateTime.UtcNow, PasswordFailuresSinceLastSuccess = defaultNumPasswordFailures, LastPasswordFailureDate = DateTime.UtcNow }; context.Users.Add(NewUser); context.SaveChanges(); return(token); } }