Rock.Model.UserLoginService.IsPasswordValid C# (CSharp) Method

IsPasswordValid() public static method

Checks to see if the given password is valid according to the PasswordRegex (if defined).
public static IsPasswordValid ( string password ) : bool
password string A password to verify.
return bool
        public static bool IsPasswordValid( string password )
        {
            var globalAttributes = GlobalAttributesCache.Read();
            string passwordRegex = globalAttributes.GetValue( "PasswordRegularExpression" );
            if ( string.IsNullOrEmpty( passwordRegex ) )
            {
                return true;
            }
            else
            {
                var regex = new Regex( passwordRegex );
                return regex.IsMatch( password );
            }
        }

Usage Example

コード例 #1
0
        /// <summary>
        /// Determines whether a new UserLogin with the specified parameters would be valid
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="passwordConfirm">The password confirm.</param>
        /// <param name="errorTitle">The error title.</param>
        /// <param name="errorMessage">The error message.</param>
        /// <returns>
        ///   <c>true</c> if [is valid new user login] [the specified user name]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsValidNewUserLogin(string userName, string password, string passwordConfirm, out string errorTitle, out string errorMessage)
        {
            errorTitle   = null;
            errorMessage = null;
            if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password))
            {
                errorTitle   = "Missing Information";
                errorMessage = "A username and password are required when saving an account";
                return(false);
            }

            if (new UserLoginService(new RockContext()).Exists(userName))
            {
                errorTitle   = "Invalid Username";
                errorMessage = "The selected Username is already being used. Please select a different Username";
                return(false);
            }

            if (!UserLoginService.IsPasswordValid(password))
            {
                errorTitle   = string.Empty;
                errorMessage = UserLoginService.FriendlyPasswordRules();
                return(false);
            }

            if (passwordConfirm != password)
            {
                errorTitle   = "Invalid Password";
                errorMessage = "The password and password confirmation do not match";
                return(false);
            }

            return(true);
        }