Share via

Issue to password

Scott Huang 3,511 Reputation points
2022-03-15T08:19:27.103+00:00

Hi,
Is there existing way in C# to validate that the relevant password does meet Windows password complexity?

Developer technologies | .NET | .NET Runtime
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2022-03-15T12:26:21.467+00:00

    If you search for .net password validator library there are a handful out there to choice from, personally I would use my own.

    For example

    public class PasswordCheck : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            var validPassword = false;
            var reason = string.Empty; // for debugging only
            var password = (value == null) ? string.Empty : value.ToString();
    
            if (string.IsNullOrWhiteSpace(password) || password.Length < 6)
            {
                reason = "new password must be at least 6 characters long. ";
            }
            else
            {
                var pattern = new Regex("((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})");
                if (!pattern.IsMatch(password))
                {
                    reason += "Your new password must contain at least 1 symbol character and number.";
                }
                else
                {
                    validPassword = true;
                }
            }
    
            return validPassword;
    
        }
    
    }
    

    And include a check for asking the user to re-enter their password e.g.

    public class CustomerLogin
    {
        /// <summary>
        /// User name
        /// </summary>
        /// <returns>User name for login</returns>
        [Required(ErrorMessage = "{0} is required"), DataType(DataType.Text)]
        [StringLength(10, MinimumLength = 6)]
        public string UserName { get; set; }
        /// <summary>
        /// User password which must match PasswordConfirmation using
        /// PasswordCheck attribute
        /// </summary>
        /// <returns>plain text password</returns>
        [Required(ErrorMessage = "{0} is required"), DataType(DataType.Text)]
        [StringLength(20, MinimumLength = 6)]
        [PasswordCheck(ErrorMessage = "Must include a number and symbol in {0}")]
        public string Password { get; set; }
        /// <summary>
        /// Confirmation of user password
        /// </summary>
        /// <returns>plain text password</returns>
        [Compare("Password", 
             ErrorMessage = "Passwords do not match, please try again"), 
         DataType(DataType.Text)]
        [StringLength(20, MinimumLength = 6)]
        public string PasswordConfirmation { get; set; }
        public override string ToString() => UserName;
    
    }
    

    Source code in the following repository, see project ValidateLogin1 for a Windows Forms example and ValidateLoginCore for a WPF example.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.