Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a .net method to check a user password for simplicity

I was wondering if there's a built-in method in C# .NET framework to check if a string, representing a new user password is too simple?

For instance, stuff like "1234" or "qwerty" or "33333" is too simple.

PS. I obviously can code all this myself. I was just curious, if there's something built into the platform that can do this for me before I begin.

like image 826
ahmd0 Avatar asked Sep 03 '25 02:09

ahmd0


2 Answers

You can use the MembershipProvider.PasswordStrengthRegularExpression property in combination with the MinRequiredPasswordLength and MinRequiredNonAlphanumericCharacters to make sure the password meets your specific needs.

like image 90
James Hull Avatar answered Sep 04 '25 14:09

James Hull


there is no built-in method in C# but you can easily setup a regular expression that checks:

At least 7 chars At least 1 uppercase char (A-Z) At least 1 number (0-9) At least one special char

public static bool IsPasswordStrong(string password)
{
    return Regex.IsMatch(password, @"^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).*$");
}
like image 28
Vivien Adnot Avatar answered Sep 04 '25 15:09

Vivien Adnot