コード例 #1
0
        /// <summary>
        /// Create a new user.
        /// </summary>
        /// <param name="username">User name.</param>
        /// <param name="password">Password.</param>
        /// <param name="email">Email address.</param>
        /// <param name="passwordQuestion">Security quesiton for password.</param>
        /// <param name="passwordAnswer">Security quesiton answer for password.</param>
        /// <param name="isApproved"></param>
        /// <param name="userID">User ID</param>
        /// <param name="status"></param>
        /// <returns>MembershipUser</returns>
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            UserBLL                   userBLL        = new UserBLL();
            ApplicationBLL            applicationBLL = new ApplicationBLL();
            ValidatePasswordEventArgs args           = new ValidatePasswordEventArgs(username, password, true);

            OnValidatingPassword(args);

            if (args.Cancel)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return(null);
            }

            MembershipUser membershipUser = GetUser(username, false);

            if (membershipUser == null)
            {
                try
                {
                    User creator = userBLL.GetUserByUserName(HttpContext.Current.User.Identity.Name);
                    User user    = new User();

                    user.UserID                                 = Guid.NewGuid();
                    user.UserName                               = username;
                    user.Password                               = EncodePassword(password);
                    user.PasswordQuestion                       = passwordQuestion;
                    user.PasswordAnswer                         = EncodePassword(passwordAnswer);
                    user.IsApproved                             = isApproved;
                    user.IsLockedOut                            = false;
                    user.IsChangePassword                       = false;
                    user.LastLoginDate                          = DateTime.Now;
                    user.LastActivityDate                       = DateTime.Now;
                    user.LastPasswordChangedDate                = DateTime.Now;
                    user.LastLockedOutDate                      = DateTime.Now;
                    user.FailedPasswordAnswerAttemptCount       = 0;
                    user.FailedPasswordAnswerAttemptWindowStart = DateTime.Now;
                    user.FailedPasswordAttemptCount             = 0;
                    user.FailedPasswordAttemptWindowStart       = DateTime.Now;
                    user.CreatedDate                            = DateTime.Now;
                    user.CreatorUserID                          = creator.UserID;
                    user.EditedDate                             = DateTime.Now;
                    user.EditorUserID                           = creator.UserID;

                    Guid ret = default(Guid);
                    ret = userBLL.CreateNewUser(user);

                    Application currentApplication = applicationBLL.GetApplicationByApplicationName(applicationName);
                    if (currentApplication == null) // If Application does not exists create it
                    {
                        currentApplication.ApplicationName = applicationName;
                        applicationBLL.CreateNewApplication(currentApplication);
                    }

                    if (ret != default(Guid))
                    {
                        status = MembershipCreateStatus.Success;
                    }
                    else
                    {
                        status = MembershipCreateStatus.UserRejected;
                    }
                }
                catch (SqlException e)
                {
                    //Add exception handling here.

                    status = MembershipCreateStatus.ProviderError;
                }

                return(GetUser(username, false));
            }
            else
            {
                status = MembershipCreateStatus.DuplicateUserName;
            }

            return(null);
        }