示例#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)
                    {
                        r.Users.Add(u);
                    }
                    r.Save();
                }
            });
        }
示例#2
0
        //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);
        }
示例#3
0
        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);
        }
示例#4
0
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            var user = DbEntryMembershipUser.FindById((long)providerUserKey);

            user.LastActivityDate = DateTime.Now;
            user.Save();
            return(user.ToMembershipUser());
        }
示例#5
0
        //
        // 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));
        }
示例#6
0
        public override string GetUserNameByEmail(string email)
        {
            var u = DbEntryMembershipUser.FindOne(CK.K["Email"] == email);

            if (u != null)
            {
                return(u.UserName);
            }
            return(null);
        }
示例#7
0
        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);
        }
示例#8
0
        private DbEntryMembershipUser FindUser(string username)
        {
            var u = DbEntryMembershipUser.FindOne(CK.K["UserName"] == username);

            if (u != null)
            {
                return(u);
            }
            return(null);
        }
示例#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());
        }
示例#10
0
        /// <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);
        }
示例#11
0
        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, int.Parse(u.PasswordSalt), u.PasswordSalt); //StringHelper.Hash(newPassword);
                u.Save();
                return(true);
            }
            return(false);
        }
示例#12
0
        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();
            }
        }
示例#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);
        }
示例#14
0
        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);
        }
示例#15
0
        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);
        }
示例#16
0
        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);
        }
示例#17
0
        /// <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);
        }
示例#18
0
        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);
        }
示例#19
0
 private bool CheckPassword(string password, DbEntryMembershipUser user)
 {
     return(user.Password.Equals(EncodePassword(password, user.PasswordFormat, user.PasswordSalt)));
 }
示例#20
0
        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();
                }
            }
        }