Hàm kiểm tra độ mạnh của mật khẩu viết bằng C#

Yêu cầu:

- Mật khẩu có độ dài tối thiểu là 10 ký tự.

- Chứa chữ cái, chữ số và kí tự đặc biệt.

- Thiết lập blacklist các mật khẩu yếu (VD: 12345@abc, 1234567a@,12345@ABC,...)

Strong Password

public static Boolean isStrongPassword(string password)
{
if (password == null)
return false;
else
return Regex.IsMatch(password, "^((?=(.*[a-zAZ]){1,})(?=(.*[\\d]){1,})(?=(.*[\\W]){1,})(?!.*\\s)).{8,}$") && !isBlacklistPassword(password);
}

private static Boolean isBlacklistPassword(string password)
{
// Hoặc có thể lưu danh sách Blacklist Password ở CSDL
string path = @"c:\Passwordblacklist.txt";
string[] fileText = File.ReadAllLines(path);
foreach (var text in fileText)
{
if (password.Equals(text)) return false;
}
return true;
}

Đăng nhận xét

Tin liên quan