public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) { if (string.IsNullOrEmpty(username)) { status = MembershipCreateStatus.InvalidUserName; return null; } if (string.IsNullOrEmpty(password)) { status = MembershipCreateStatus.InvalidPassword; return null; } if (string.IsNullOrEmpty(email)) { status = MembershipCreateStatus.InvalidEmail; return null; } string HashedPassword = Crypto.HashPassword(password); if (HashedPassword.Length > 128) { status = MembershipCreateStatus.InvalidPassword; return null; } using (LibraryContext Context = new LibraryContext()) { if (Context.Users.Where(Usr => Usr.Username == username).Any()) { status = MembershipCreateStatus.DuplicateUserName; return null; } if (Context.Users.Where(Usr => Usr.Email == email).Any()) { status = MembershipCreateStatus.DuplicateEmail; return null; } User NewUser = new User { Id = Guid.NewGuid(), Username = username, Password = HashedPassword, IsApproved = isApproved, Email = email, CreateDate = DateTime.UtcNow, LastPasswordChangedDate = DateTime.UtcNow, PasswordFailuresSinceLastSuccess = 0, LastLoginDate = DateTime.UtcNow, LastActivityDate = DateTime.UtcNow, LastLockoutDate = DateTime.UtcNow, IsLockedOut = false, LastPasswordFailureDate = DateTime.UtcNow }; Context.Users.Add(NewUser); var role = Context.Roles.FirstOrDefault(x => x.RoleName == "reader"); role.Users.Add(NewUser); Context.SaveChanges(); status = MembershipCreateStatus.Success; return new MembershipUser(System.Web.Security.Membership.Provider.Name, NewUser.Username, NewUser.Id, NewUser.Email, null, null, NewUser.IsApproved, NewUser.IsLockedOut, NewUser.CreateDate.Value, NewUser.LastLoginDate.Value, NewUser.LastActivityDate.Value, NewUser.LastPasswordChangedDate.Value, NewUser.LastLockoutDate.Value); } }
public string CreateAccount(string userName, string password, bool requireConfirmationToken) { if (string.IsNullOrEmpty(userName)) { throw new MembershipCreateUserException(MembershipCreateStatus.InvalidUserName); } if (string.IsNullOrEmpty(password)) { throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword); } string hashedPassword = Crypto.HashPassword(password); if (hashedPassword.Length > 128) { throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword); } using (LibraryContext Context = new LibraryContext()) { if (Context.Users.Where(Usr => Usr.Username == userName).Any()) { throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName); } string token = string.Empty; if (requireConfirmationToken) { token = GenerateToken(); } User NewUser = new User { Id = Guid.NewGuid(), Username = userName, Password = hashedPassword, IsApproved = !requireConfirmationToken, Email = string.Empty, CreateDate = DateTime.UtcNow, LastPasswordChangedDate = DateTime.UtcNow, PasswordFailuresSinceLastSuccess = 0, LastLoginDate = DateTime.UtcNow, LastActivityDate = DateTime.UtcNow, LastLockoutDate = DateTime.UtcNow, IsLockedOut = false, LastPasswordFailureDate = DateTime.UtcNow, ConfirmationToken = token }; Context.Users.Add(NewUser); Context.SaveChanges(); return token; } }