예제 #1
0
 public static System.Web.Security.MembershipUser Map(string pname, User user, bool EFMembership)
 {
     if (EFMembership)
         return new MembershipUser(pname, user.Username, user.UserID, user.Email, user.PasswordQuestion, user.Comment, user.IsApproved,
                                   user.Status == 2, user.CreateOn, user.LastLoginDate, user.LastActivityDate, user.LastPasswordChangedDate,
                                   user.LastLockoutDate, user.FirstName, user.LastName, user.TimeZone.GetValueOrDefault(0));
     else
         return new System.Web.Security.MembershipUser(pname, user.Username, user.UserID, user.Email, user.PasswordQuestion, user.Comment, user.IsApproved,
                                                       user.Status == 2, user.CreateOn, user.LastLoginDate, user.LastActivityDate, user.LastPasswordChangedDate,
                                                       user.LastLockoutDate);
 }
 /// <summary>
 /// Create a new User object.
 /// </summary>
 /// <param name="userID">Initial value of the UserID property.</param>
 /// <param name="username">Initial value of the Username property.</param>
 /// <param name="isAnonymous">Initial value of the IsAnonymous property.</param>
 /// <param name="lastActivityDate">Initial value of the LastActivityDate property.</param>
 /// <param name="password">Initial value of the Password property.</param>
 /// <param name="passwordFormat">Initial value of the PasswordFormat property.</param>
 /// <param name="passwordSalt">Initial value of the PasswordSalt property.</param>
 /// <param name="isApproved">Initial value of the IsApproved property.</param>
 /// <param name="createOn">Initial value of the CreateOn property.</param>
 /// <param name="lastLoginDate">Initial value of the LastLoginDate property.</param>
 /// <param name="lastPasswordChangedDate">Initial value of the LastPasswordChangedDate property.</param>
 /// <param name="lastLockoutDate">Initial value of the LastLockoutDate property.</param>
 /// <param name="failedPasswordAttemptCount">Initial value of the FailedPasswordAttemptCount property.</param>
 /// <param name="failedPasswordAttemptWindowStart">Initial value of the FailedPasswordAttemptWindowStart property.</param>
 /// <param name="failedPasswordAnswerAttemptCount">Initial value of the FailedPasswordAnswerAttemptCount property.</param>
 /// <param name="failedPasswordAnswerAttemptWindowStart">Initial value of the FailedPasswordAnswerAttemptWindowStart property.</param>
 /// <param name="comment">Initial value of the Comment property.</param>
 /// <param name="status">Initial value of the Status property.</param>
 public static User CreateUser(global::System.Int32 userID, global::System.String username, global::System.Boolean isAnonymous, global::System.DateTime lastActivityDate, global::System.String password, global::System.Int32 passwordFormat, global::System.String passwordSalt, global::System.Boolean isApproved, global::System.DateTime createOn, global::System.DateTime lastLoginDate, global::System.DateTime lastPasswordChangedDate, global::System.DateTime lastLockoutDate, global::System.Int32 failedPasswordAttemptCount, global::System.DateTime failedPasswordAttemptWindowStart, global::System.Int32 failedPasswordAnswerAttemptCount, global::System.DateTime failedPasswordAnswerAttemptWindowStart, global::System.String comment, global::System.Byte status)
 {
     User user = new User();
     user.UserID = userID;
     user.Username = username;
     user.IsAnonymous = isAnonymous;
     user.LastActivityDate = lastActivityDate;
     user.Password = password;
     user.PasswordFormat = passwordFormat;
     user.PasswordSalt = passwordSalt;
     user.IsApproved = isApproved;
     user.CreateOn = createOn;
     user.LastLoginDate = lastLoginDate;
     user.LastPasswordChangedDate = lastPasswordChangedDate;
     user.LastLockoutDate = lastLockoutDate;
     user.FailedPasswordAttemptCount = failedPasswordAttemptCount;
     user.FailedPasswordAttemptWindowStart = failedPasswordAttemptWindowStart;
     user.FailedPasswordAnswerAttemptCount = failedPasswordAnswerAttemptCount;
     user.FailedPasswordAnswerAttemptWindowStart = failedPasswordAnswerAttemptWindowStart;
     user.Comment = comment;
     user.Status = status;
     return user;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the Users EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToUsers(User user)
 {
     base.AddObject("Users", user);
 }
        public override System.Web.Security.MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out System.Web.Security.MembershipCreateStatus status)
        {
            if (!ValidateParameter(ref password, true, true, false, 128))
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            var salt = this.GenerateSalt();
            var pass = this.EncodePassword(password, (int)this._PasswordFormat, salt);
            if (pass.Length > 128)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            string encodedPasswordAnswer;
            if (passwordAnswer != null)
            {
                passwordAnswer = passwordAnswer.Trim();
            }

            if (!string.IsNullOrEmpty(passwordAnswer))
            {
                if (passwordAnswer.Length > 128)
                {
                    status = MembershipCreateStatus.InvalidAnswer;
                    return null;
                }
                encodedPasswordAnswer = this.EncodePassword(passwordAnswer.ToLower(CultureInfo.InvariantCulture), (int)this._PasswordFormat, salt);
            }
            else
                encodedPasswordAnswer = passwordAnswer;

            if (!ValidateParameter(ref encodedPasswordAnswer, this.RequiresQuestionAndAnswer, true, false, 128))
            {
                status = MembershipCreateStatus.InvalidAnswer;
                return null;
            }

            if (!ValidateParameter(ref username, true, true, true, 256))
            {
                status = MembershipCreateStatus.InvalidUserName;
                return null;
            }

            if (!ValidateParameter(ref email,
                                               this.RequiresUniqueEmail,
                                               this.RequiresUniqueEmail,
                                               false,
                                               256))
            {
                status = MembershipCreateStatus.InvalidEmail;
                return null;
            }

            if (!ValidateParameter(ref passwordQuestion, this.RequiresQuestionAndAnswer, true, false, 256))
            {
                status = MembershipCreateStatus.InvalidQuestion;
                return null;
            }

            if (providerUserKey != null)
            {
                //if (!(providerUserKey is Guid)) {
                //    status = MembershipCreateStatus.InvalidProviderUserKey;
                //    return null;
                //}
                status = MembershipCreateStatus.InvalidProviderUserKey;
                return null;
            }

            if (password.Length < this.MinRequiredPasswordLength)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            int count = 0;

            for (int i = 0; i < password.Length; i++)
            {
                if (!char.IsLetterOrDigit(password, i))
                {
                    count++;
                }
            }

            if (count < this.MinRequiredNonAlphanumericCharacters)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            if (this.PasswordStrengthRegularExpression.Length > 0)
            {
                if (!Regex.IsMatch(password, this.PasswordStrengthRegularExpression))
                {
                    status = MembershipCreateStatus.InvalidPassword;
                    return null;
                }
            }

            ValidatePasswordEventArgs e = new ValidatePasswordEventArgs(username, password, true);
            this.OnValidatingPassword(e);

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

            using (var db = this.Data)
            {
                if (this._UseSP)
                {
                    var userId = new ObjectParameter("UserID", typeof(int));
                    var time = DateTime.UtcNow;
                    var pStatus = new ObjectParameter("RETURN_VALUE", typeof(int));
                    var cStatus = db.Membership_CreateUser(this.ApplicationName,
                                                           username,
                                                           pass,
                                                           salt,
                                                           email,
                                                           passwordQuestion,
                                                           passwordAnswer,
                                                           isApproved,
                                                           false,
                                                           0,
                                                           time,
                                                           this.RequiresUniqueEmail,
                                                           (int)this.PasswordFormat,
                                                           userId).FirstOrDefault();

                    if ((cStatus < 0) || (cStatus > 11))
                    {
                        cStatus = 11;
                    }
                    status = (MembershipCreateStatus)cStatus;
                    if (cStatus != 0)
                    {
                        return null;
                    }

                    return new MembershipUser(this.Name, username, userId.Value, email, passwordQuestion, null, isApproved, false, time, time, time, time, new DateTime(0x6da, 1, 1), null, null, 0);
                }
                else
                {
                    if (this.RequiresUniqueEmail)
                    {
                        if (db.Users.Where(u => u.Email == email && u.Application.ApplicationID == this._AppID).Any())
                        {
                            status = MembershipCreateStatus.DuplicateEmail;
                            return null;
                        }
                    }

                    if (db.Users.Where(u => u.Username == username && u.Application.ApplicationID == this._AppID).Any())
                    {
                        status = MembershipCreateStatus.DuplicateUserName;
                        return null;
                    }

                    var utc = DateTime.UtcNow;
                    var user = new User()
                    {
                        Comment = "",
                        CreateOn = utc,
                        Email = email,
                        FailedPasswordAnswerAttemptCount = 0,
                        FailedPasswordAnswerAttemptWindowStart = utc,
                        FailedPasswordAttemptCount = 0,
                        FailedPasswordAttemptWindowStart = utc,
                        IsAnonymous = false,
                        IsApproved = isApproved,
                        LastActivityDate = utc,
                        LastLockoutDate = utc,
                        LastLoginDate = utc,
                        LastPasswordChangedDate = utc,
                        Password = pass,
                        PasswordAnswer = encodedPasswordAnswer,
                        PasswordFormat = (int)this.PasswordFormat,
                        PasswordQuestion = passwordQuestion,
                        PasswordSalt = salt,
                        TimeZone = 0,
                        Username = username,
                        Application = db.GetApplication(this._AppID)
                    };

                    db.AddToUsers(user);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        status = MembershipCreateStatus.UserRejected;
                        return null;
                    }

                    status = MembershipCreateStatus.Success;
                    return UserMapper.Map(this.Name, user, this._ReturnEFMembershipUser);
                }
            }
        }
        private bool CheckPassword(Entities db, string username, string password, bool updateLastLoginActivityDate, bool failIfNotApproved, out string salt, out int passwordFormat, out User usr)
        {
            var user = this.GetDBUser(db, username);

            usr = user;
            if (user == null)
            {
                salt = null;
                passwordFormat = -1;

                return false;
            }

            var enc = this.EncodePassword(password, user.PasswordFormat, user.PasswordSalt);
            passwordFormat = user.PasswordFormat;
            salt = user.PasswordSalt;
            if (enc == user.Password)
            {
                if (updateLastLoginActivityDate)
                {
                    if (this._UseSP)
                        db.Membership_SetUserLoginDate(user.UserID, DateTime.Now);
                    else
                    {
                        user.LastActivityDate = DateTime.Now;
                        user.LastLoginDate = DateTime.Now;

                        db.SaveChanges();
                    }
                }
                return true;
            }
            else
                return false;
        }
 private bool CheckPassword(Entities db, string username, string password, bool updateLastLoginActivityDate, bool failIfNotApproved, out User usr)
 {
     string salt;
     int passwordFormat;
     return this.CheckPassword(db, username, password, updateLastLoginActivityDate, failIfNotApproved, out salt, out passwordFormat, out usr);
 }