Exemple #1
0
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            Condition c = null;

            foreach (string s in roleNames)
            {
                c |= CK.K["Name"] == s;
            }
            Condition cu = null;

            foreach (string s in usernames)
            {
                cu |= CK.K["UserName"] == s;
            }
            DbEntry.UsingTransaction(delegate
            {
                List <DbEntryMembershipUser> ls = DbEntryMembershipUser.Find(cu);
                foreach (DbEntryRole r in DbEntryRole.Find(c))
                {
                    foreach (DbEntryMembershipUser u in ls)
                    {
                        if (!r.Users.Contains(u))
                        {
                            r.Users.Add(u);
                        }
                    }
                    r.Save();
                }
            });
        }
        //public override int MaxInvalidPasswordAttempts
        //{
        //    get { return WebSettings.MaxInvalidPasswordAttempts; }
        //}

        //public override int MinRequiredNonAlphanumericCharacters
        //{
        //    get { return WebSettings.MinRequiredNonAlphanumericCharacters; }
        //}

        //public override int MinRequiredPasswordLength
        //{
        //    get { return WebSettings.MinRequiredPasswordLength; }
        //}

        //public override int PasswordAttemptWindow
        //{
        //    get { return WebSettings.PasswordAttemptWindow; }
        //}

        //public override MembershipPasswordFormat PasswordFormat
        //{
        //    get { return MembershipPasswordFormat.Hashed; }
        //}

        //public override string PasswordStrengthRegularExpression
        //{
        //    get { return WebSettings.PasswordStrengthRegularExpression; }
        //}

        //public override bool RequiresQuestionAndAnswer
        //{
        //    get { return _requiresQuestionAndAnswer; }
        //}

        //public override bool RequiresUniqueEmail
        //{
        //    get { return true; }
        //}

        public override string ResetPassword(string username, string answer)
        {
            if (!EnablePasswordReset)
            {
                throw new NotSupportedException("Password reset is not enabled.");
            }

            DbEntryMembershipUser u = DbEntryMembershipUser.FindOne(p => p.UserName == username);

            if (answer == null && RequiresQuestionAndAnswer)
            {
                UpdateFailureCount(u, "passwordAnswer");

                throw new ProviderException("Password answer required for password reset.");
            }

            string newPassword =
                System.Web.Security.Membership.GeneratePassword(NewPasswordLength, MinRequiredNonAlphanumericCharacters);

            //    var u = DbEntryMembershipUser.FindOne(CK.K["UserName"] == username);
            if (u != null)
            {
                if (!RequiresQuestionAndAnswer || this.CheckPassword(u.PasswordAnswer, u))           //还有错误
                {
                    u.Password = this.EncodePassword(newPassword, u.PasswordFormat, u.PasswordSalt); ////StringHelper.Hash(p);
                    u.Save();
                    return(newPassword);
                }
            }
            return(null);
        }
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            var args = new ValidatePasswordEventArgs(username, password, true);

            OnValidatingPassword(args);

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

            if (RequiresUniqueEmail && !string.IsNullOrWhiteSpace(GetUserNameByEmail(email)))
            {
                status = MembershipCreateStatus.DuplicateEmail;
                return(null);
            }

            MembershipUser u = GetUser(username, false);

            if (u == null)
            {
                DateTime createDate = DateTime.Now;

                passwordQuestion = string.IsNullOrWhiteSpace(passwordQuestion) ? string.Empty : passwordQuestion;
                passwordAnswer   = string.IsNullOrWhiteSpace(passwordAnswer) ? string.Empty : passwordAnswer;
                string salt = GenerateSalt();
                var    user = new DbEntryMembershipUser
                {
                    UserName                = username,
                    Password                = EncodePassword(password, (int)this.PasswordFormat, salt),
                    PasswordFormat          = PasswordFormat.GetHashCode(),
                    PasswordSalt            = salt,
                    Email                   = email,
                    LoweredEmail            = email == null ? string.Empty : email.ToLowerInvariant(),
                    PasswordQuestion        = passwordQuestion,
                    PasswordAnswer          = passwordAnswer,
                    IsApproved              = isApproved,
                    IsLockedOut             = false,
                    LastLoginDate           = createDate,
                    LastPasswordChangedDate = createDate,
                    LastLockoutDate         = createDate,
                    FailedCount             = 0,
                    FailedStart             = createDate,
                    FailedAnswerCount       = 0,
                    FailedAnswerStart       = createDate,
                    LastActivityDate        = createDate,
                    Comment                 = string.Empty
                };
                //.Init(username, password, email, passwordQuestion, passwordAnswer, isApproved, null);
                //     user.CreateDate = createDate;
                user.Save();
                status = MembershipCreateStatus.Success;
                return(GetUser(username, false));
            }
            status = MembershipCreateStatus.DuplicateUserName;

            return(null);
        }
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            var user = DbEntryMembershipUser.FindById((long)providerUserKey);

            user.LastActivityDate = DateTime.Now;
            user.Save();
            return(user.ToMembershipUser());
        }
        //
        // MembershipProvider.GetNumberOfUsersOnline
        //

        public override int GetNumberOfUsersOnline()
        {
            var      onlineSpan  = new TimeSpan(0, System.Web.Security.Membership.UserIsOnlineTimeWindow, 0);
            DateTime compareTime = DateTime.Now.Subtract(onlineSpan);

            long numOnline = DbEntryMembershipUser.GetCount(p => p.LastActivityDate > compareTime);

            return(Convert.ToInt32(numOnline));
        }
        public override string GetUserNameByEmail(string email)
        {
            var u = DbEntryMembershipUser.FindOne(CK.K["Email"] == email);

            if (u != null)
            {
                return(u.UserName);
            }
            return(null);
        }
        private DbEntryMembershipUser FindUser(string username, string password)
        {
            var u = DbEntryMembershipUser.FindOne(CK.K["UserName"] == username);

            if (u != null && CheckPassword(password, u))
            {
                return(u);
            }
            return(null);
        }
        private DbEntryMembershipUser FindUser(string username)
        {
            var u = DbEntryMembershipUser.FindOne(CK.K["UserName"] == username);

            if (u != null)
            {
                return(u);
            }
            return(null);
        }
Exemple #9
0
        public override string[] GetRolesForUser(string username)
        {
            var u  = DbEntryMembershipUser.FindOne(CK.K["UserName"] == username);
            var ls = new List <string>();

            foreach (DbEntryRole r in u.Roles)
            {
                ls.Add(r.Name);
            }
            return(ls.ToArray());
        }
        /// <summary>
        /// 快速重置密码
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password"> </param>
        /// <returns>成功返回true,否则返回false</returns>
        public bool ResetPasswordX(string username, string password)
        {
            DbEntryMembershipUser u = DbEntryMembershipUser.FindOne(p => p.UserName == username);

            if (u != null)
            {
                u.Password = this.EncodePassword(password, u.PasswordFormat, u.PasswordSalt); ////StringHelper.Hash(p);
                u.Save();
                return(true);
            }
            return(false);
        }
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            var u = DbEntryMembershipUser.FindOne(CK.K["UserName"] == username);

            if (u != null && this.CheckPassword(oldPassword, u))
            {
                u.Password = this.EncodePassword(newPassword, u.PasswordFormat, u.PasswordSalt); //StringHelper.Hash(newPassword);
                u.Save();
                return(true);
            }
            return(false);
        }
        public override void UpdateUser(MembershipUser user)
        {
            var u = DbEntryMembershipUser.FindById((long)user.ProviderUserKey);

            if (u != null)
            {
                u.Email            = user.Email;
                u.PasswordQuestion = user.PasswordQuestion;
                u.IsApproved       = user.IsApproved;
                u.Comment          = user.Comment;
                u.Save();
            }
        }
Exemple #13
0
        public override bool IsUserInRole(string username, string roleName)
        {
            var u = DbEntryMembershipUser.FindOne(CK.K["UserName"] == username);

            foreach (DbEntryRole r in u.Roles)
            {
                if (r.Name == roleName)
                {
                    return(true);
                }
            }
            return(false);
        }
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            if (deleteAllRelatedData)
            {
                throw new DataException("Not support deleteAllRelatedData");
            }
            var u = DbEntryMembershipUser.FindOne(CK.K["UserName"] == username);

            if (u != null)
            {
                u.Delete();
                return(true);
            }
            return(false);
        }
        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            var u = DbEntryMembershipUser.FindOne(CK.K["UserName"] == username);

            if (u != null)
            {
                if (userIsOnline)
                {
                    u.LastActivityDate = DateTime.Now;
                    u.Save();
                }
                return(u.ToMembershipUser());
            }
            return(null);
        }
        public override bool UnlockUser(string userName)
        {
            //    throw new Exception("The method or operation is not implemented.");
            var u = DbEntryMembershipUser.FindOne(p => p.UserName == userName);

            if (u != null)
            {
                u.IsLockedOut       = false;
                u.FailedAnswerCount = 0;
                u.FailedCount       = 0;
                u.Save();
                return(true);
            }
            return(false);
        }
        /// <summary>
        /// 快速重置密码
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>新密码</returns>
        public string ResetPassword(string username)
        {
            DbEntryMembershipUser u = DbEntryMembershipUser.FindOne(p => p.UserName == username);

            string newPassword =
                System.Web.Security.Membership.GeneratePassword(NewPasswordLength, MinRequiredNonAlphanumericCharacters);

            if (u != null)
            {
                u.Password = this.EncodePassword(newPassword, u.PasswordFormat, u.PasswordSalt); ////StringHelper.Hash(p);
                u.Save();
                return(newPassword);
            }
            return(null);
        }
        public override string GetPassword(string username, string answer)
        {
            if (!EnablePasswordRetrieval)
            {
                throw new ProviderException("Password Retrieval Not Enabled.");
            }

            if (PasswordFormat == MembershipPasswordFormat.Hashed)
            {
                throw new ProviderException("Cannot retrieve Hashed passwords.");
            }

            string password = "";

            var u = DbEntryMembershipUser.FindOne(CK.K["UserName"] == username);

            if (u == null)
            {
                throw new MembershipPasswordException("Can not find the user.");
            }
            if (u.IsLockedOut)
            {
                throw new MembershipPasswordException("The supplied user is locked out.");
            }

            if (RequiresQuestionAndAnswer && !CheckPassword(answer, u))
            {
                UpdateFailureCount(u, "passwordAnswer");

                throw new MembershipPasswordException("Incorrect password answer.");
            }

            if (PasswordFormat == MembershipPasswordFormat.Encrypted)
            {
                password = UnEncodePassword(password);
            }

            return(password);
        }
 private bool CheckPassword(string password, DbEntryMembershipUser user)
 {
     return(user.Password.Equals(EncodePassword(password, user.PasswordFormat, user.PasswordSalt)));
 }
        private void UpdateFailureCount(DbEntryMembershipUser user, string failureType)
        {
            var windowStart  = new DateTime();
            int failureCount = 0;

            if (failureType == "password")
            {
                failureCount = user.FailedCount;
                try
                {
                    windowStart = user.FailedStart;
                }
                catch
                {
                    windowStart = DateTime.Now;
                }
            }

            if (failureType == "passwordAnswer")
            {
                failureCount = user.FailedAnswerCount;
                windowStart  = user.FailedAnswerStart;
            }

            var windowEnd = windowStart.AddMinutes(PasswordAttemptWindow);
            var utcNow    = DateTime.Now;

            if (failureCount == 0 || utcNow > windowEnd)
            {
                if (failureType == "password")
                {
                    user.FailedCount = 1;
                    user.FailedStart = DateTime.Now;
                    user.Save();
                }
                if (failureType == "passwordAnswer")
                {
                    user.FailedAnswerCount = 1;
                    user.FailedAnswerStart = DateTime.Now;
                    user.Save();
                }
            }
            else
            {
                if (failureCount++ >= MaxInvalidPasswordAttempts)
                {
                    user.IsLockedOut     = true;
                    user.LastLockoutDate = DateTime.Now;
                    user.Save();
                }
                else
                {
                    if (failureType == "password")
                    {
                        user.FailedCount = failureCount;
                    }

                    if (failureType == "passwordAnswer")
                    {
                        user.FailedAnswerCount = failureCount;
                    }
                    user.Save();
                }
            }
        }