Created
May 27, 2010 01:02
-
-
Save xeb/415288 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text.RegularExpressions; | |
//Tony ***** H19 08/24/2009 | |
namespace App_Code | |
{ | |
public class PasswordStrength | |
{ | |
public string DoesPasswordMeetBusinessRules(string strPasswordToTest) | |
{ | |
// Password Rules *************************************** | |
// 1. Minimum length of 7 characters. | |
// 2. Contain at least 1 alpha-character and 1 number. | |
// 3. No character can occur 3 times consecutively. | |
// ****************************************************** | |
if (strPasswordToTest == "") | |
{ | |
return ""; | |
} | |
string strPasswordRuleCheckResult = ""; | |
bool blnIsPasswordOK = true; | |
bool blnIsDigit = true; | |
bool blnIsAlpha = true; | |
bool blnIsLength8 = true; | |
bool bln3ofSameCharsInARow = false; | |
blnIsDigit = Regex.IsMatch(strPasswordToTest, @"\d"); | |
if (!blnIsDigit) | |
{ | |
strPasswordRuleCheckResult = strPasswordRuleCheckResult + "New password choice is required to contain at least one number. "; | |
blnIsPasswordOK = false; | |
} | |
blnIsAlpha = Regex.IsMatch(strPasswordToTest, @"\D"); | |
if (!blnIsAlpha) | |
{ | |
strPasswordRuleCheckResult = strPasswordRuleCheckResult + "New password choice is required to contain at least on letter. "; | |
blnIsPasswordOK = false; | |
} | |
blnIsLength8 = Regex.IsMatch(strPasswordToTest, @".{8,}"); | |
if (!blnIsLength8) | |
{ | |
strPasswordRuleCheckResult = strPasswordRuleCheckResult + "New password choice is required to contain at least 8 characters. "; | |
blnIsPasswordOK = false; | |
} | |
bln3ofSameCharsInARow = Regex.IsMatch(strPasswordToTest, @"!{3,}|\""{3,}|#{3,}|\${3,}|%{3,}|&{3,}|'{3,}|\({3,}|\){3,}|\*{3,}|\+{3,}|,{3,}|-{3,}|\.{3,}|/{3,}|0{3,}|1{3,}|3{3,}|3{3,}|4{3,}|3{3,}|6{3,}|7{3,}|8{3,}|9{3,}|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|
{ | |
strPasswordRuleCheckResult = strPasswordRuleCheckResult + "No character can occur more than 2 times consecutively in the new password choice. "; | |
blnIsPasswordOK = false; | |
} | |
if (blnIsPasswordOK) | |
{ | |
return "PasswordIsOK"; | |
} | |
else | |
{ | |
return strPasswordRuleCheckResult; | |
} | |
} | |
} | |
} | |
//Tony ***** H19 08/24/2009 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment