Пример #1
0
        /// <summary>
        /// Reset a users password - *
        /// </summary>
        /// <param name="username">User to be found based by Name</param>
        /// <param name="answer">Verifcation that it is them</param>
        /// <returns>Username as string</returns>
        public override string ResetPassword(string username, string answer)
        {
            string newPassword = string.Empty, newPasswordEnc = string.Empty, newPasswordSalt = string.Empty, newPasswordAnswer = string.Empty;

            /// Check Password reset is enabled
            if (!(this.EnablePasswordReset))
            {
                ExceptionReporter.ThrowNotSupported("MEMBERSHIP", "RESETNOTSUPPORTED");
            }

            // Check arguments for null values
            if (username == null)
            {
                ExceptionReporter.ThrowArgument("MEMBERSHIP", "USERNAMEPASSWORDNULL");
            }

            // get an instance of the current password information class
            UserPasswordInfo currentPasswordInfo = UserPasswordInfo.CreateInstanceFromDB(this.ApplicationName, username, false, this.UseSalt);

            if (currentPasswordInfo != null)
            {
                if (UseSalt && String.IsNullOrEmpty(currentPasswordInfo.PasswordSalt))
                {
                    // get a new password salt...
                    newPasswordSalt = YafMembershipProvider.GenerateSalt();
                }
                else
                {
                    // use existing salt...
                    newPasswordSalt = currentPasswordInfo.PasswordSalt;
                }

                if (!String.IsNullOrEmpty(answer))
                {
                    // verify answer is correct...
                    if (!currentPasswordInfo.IsCorrectAnswer(answer))
                    {
                        return(null);
                    }
                }

                // create a new password
                newPassword = YafMembershipProvider.GeneratePassword(this.MinRequiredPasswordLength, this.MinRequiredNonAlphanumericCharacters);
                // encode it...
                newPasswordEnc = YafMembershipProvider.EncodeString(newPassword, ( int )this.PasswordFormat, newPasswordSalt, this.UseSalt);
                // save to the database
                DB.ResetPassword(this.ApplicationName, username, newPasswordEnc, newPasswordSalt, ( int )this.PasswordFormat, this.MaxInvalidPasswordAttempts, this.PasswordAttemptWindow);
                // Return unencrypted password
                return(newPassword);
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Retrieves the Users password (if EnablePasswordRetrieval is true)
        /// </summary>
        /// <param name="username">Username to retrieve password for</param>
        /// <param name="answer">Answer to the Users Membership Question</param>
        /// <param name="newPasswordQuestion">New question</param>
        /// <param name="newPasswordAnswer">New answer</param>
        /// <returns> Password unencrypted</returns>
        public override string GetPassword(string username, string answer)
        {
            if (!this.EnablePasswordRetrieval)
            {
                ExceptionReporter.ThrowNotSupported("MEMBERSHIP", "PASSWORDRETRIEVALNOTSUPPORTED");
            }

            // Check for null arguments
            if ((username == null) || (answer == null))
            {
                ExceptionReporter.ThrowArgument("MEMBERSHIP", "USERNAMEPASSWORDNULL");
            }

            UserPasswordInfo currentPasswordInfo = UserPasswordInfo.CreateInstanceFromDB(this.ApplicationName, username, false, this.UseSalt);

            if (currentPasswordInfo != null && currentPasswordInfo.IsCorrectAnswer(answer))
            {
                return(YafMembershipProvider.DecodeString(currentPasswordInfo.Password, currentPasswordInfo.PasswordFormat));
            }

            return(null);
        }