Esempio n. 1
0
 public void Save()
 {
     context.SaveChanges();
 }
        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 (CodeWickContext Context = new CodeWickContext()) {
                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 {
                    UserId     = 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);
                Context.SaveChanges();
                status = MembershipCreateStatus.Success;
                return(new MembershipUser(Membership.Provider.Name, NewUser.Username, NewUser.UserId, NewUser.Email, null, null, NewUser.IsApproved, NewUser.IsLockedOut, NewUser.CreateDate.Value, NewUser.LastLoginDate.Value, NewUser.LastActivityDate.Value, NewUser.LastPasswordChangedDate.Value, NewUser.LastLockoutDate.Value));
            }
        }