MyPasswordStrength 4.0.0
dotnet add package MyPasswordStrength --version 4.0.0
NuGet\Install-Package MyPasswordStrength -Version 4.0.0
<PackageReference Include="MyPasswordStrength" Version="4.0.0" />
<PackageVersion Include="MyPasswordStrength" Version="4.0.0" />
<PackageReference Include="MyPasswordStrength" />
paket add MyPasswordStrength --version 4.0.0
#r "nuget: MyPasswordStrength, 4.0.0"
#:package MyPasswordStrength@4.0.0
#addin nuget:?package=MyPasswordStrength&version=4.0.0
#tool nuget:?package=MyPasswordStrength&version=4.0.0
MyPasswordStrength
Define your password strength complexity requirements with ease using the library.
The package provides a Validator class that you can use to validate passwords programmatically.
The Validator supports multilingual password strength validation too.
You can configure:
- Desired language
- Minimum length
- Minimum upper case characters
- Minimum lower case characters
- Minimum digits
- Minimum special characters
- Maximum same consecutive characters - eg aaa
- Maximum consecutive ascending and/or descending digits - eg 123 / 654
- Maximum consecutive ascending and/or descending characters - eg aBCd / DcbA
- Repeated sequence check - eg in P@ssword@s - @s is repeating sequence
Programmatic Password Validation
You can validate passwords programmatically using the PasswordStrengthValidator class provided in the package.
You can set the password strength requirements through the properties of the PasswordStrengthValidator class,
and then call the PasswordStrength method to check if a given password meets those requirements.
The PasswordStrength method returns a boolean indicating whether the password is valid according to the configured requirements.
Sample Usage
// Configure password strength requirements
var validator = new PasswordStrengthValidator
{
MinimumLength = 9,
RequireUppercase = true,
MinUppercase = 2,
RequireLowercase = true,
MinLowercase = 3,
RequireDigit = true,
MinDigit = 2,
RequireSpecialCharacter = true,
MinSpecialCharacter = 2,
RequireMaxNoOfSameConsecutiveCharacters = true,
MaxNoOfSameConsecutiveCharacters = 2,
RequireMaxNoOfConsecutiveAscendingDigits = true,
MaxNoOfConsecutiveAscendingDigits = MaxNoOfConsecutiveDigits.Three,
RequireMaxNoOfConsecutiveDescendingDigits = true,
MaxNoOfConsecutiveDescendingDigits = MaxNoOfConsecutiveDigits.Two,
RequireMaxNoOfConsecutiveAscendingCharacters = true,
MaxNoOfConsecutiveAscendingCharacters = MaxNoOfConsecutiveCharacters.Three,
RequireMaxNoOfConsecutiveDescendingCharacters = true,
MaxNoOfConsecutiveDescendingCharacters = MaxNoOfConsecutiveCharacters.Two,
RequireRepeatingSequenceCheck = true,
MinLengthOfRepeatingSequence = 2
};
var password = "P@76abc0rDed123!";
// Validate the password
bool isValid = validator.PasswordStrength(password);
if (isValid)
{
Console.WriteLine("Password meets the strength requirements.");
}
else
{
Console.WriteLine("Password does not meet the strength requirements.");
}
The special characters considered in the validation are: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~.
You can modify this set of special characters by setting the SpecialCharacters property to a custom string of special characters.
Sample ASP .NET Core Password Strength Data Annotation
You can use the Validator class to create a custom data annotation attribute for password strength validation in your ASP .NET Core applications.
Sample data annotation
using MyPasswordStrength;
using System.ComponentModel.DataAnnotations;
namespace YourNamespace
{
public class PasswordStrengthAttribute : RegularExpressionAttribute
{
public PasswordStrengthAttribute(int minimumLength = 6, bool requireUppercase = true, int minUppercase = 1,
bool requireLowercase = true, int minLowercase = 1, bool requireDigit = true, int minDigit = 1,
bool requireSpecialCharacter = true, int minSpecialCharacter = 1, string specialCharacters = @"!""#$%&'()*+,-./:;<=>?@[\]^_`{|}~",
bool requireMaxNoOfSameConsecutiveCharacters = true, int maxNoOfSameConsecutiveCharacters = 2,
bool requireMaxNoOfConsecutiveAscendingDigits = true, MaxNoOfConsecutiveDigits maxNoOfConsecutiveAscendingDigits = MaxNoOfConsecutiveDigits.Two,
bool requireMaxNoOfConsecutiveDescendingDigits = true, MaxNoOfConsecutiveDigits maxNoOfConsecutiveDescendingDigits = MaxNoOfConsecutiveDigits.Two,
bool requireMaxNoOfConsecutiveAscendingCharacters = true, MaxNoOfConsecutiveCharacters maxNoOfConsecutiveAscendingCharacters = MaxNoOfConsecutiveCharacters.Two,
bool requireMaxNoOfConsecutiveDescendingCharacters = true, MaxNoOfConsecutiveCharacters maxNoOfConsecutiveDescendingCharacters = MaxNoOfConsecutiveCharacters.Two,
bool requireRepeatingSequenceCheck = true, int minLengthOfRepeatingSequence = 2,
Language language = Language.English)
: base(PasswordStrengthValidator.GetRegexPattern(minimumLength, requireUppercase, minUppercase, requireLowercase, minLowercase,
requireDigit, minDigit, requireSpecialCharacter, minSpecialCharacter, specialCharacters,
requireMaxNoOfSameConsecutiveCharacters, maxNoOfSameConsecutiveCharacters,
requireMaxNoOfConsecutiveAscendingDigits, maxNoOfConsecutiveAscendingDigits,
requireMaxNoOfConsecutiveDescendingDigits, maxNoOfConsecutiveDescendingDigits,
requireMaxNoOfConsecutiveAscendingCharacters, maxNoOfConsecutiveAscendingCharacters,
requireMaxNoOfConsecutiveDescendingCharacters, maxNoOfConsecutiveDescendingCharacters,
requireRepeatingSequenceCheck, minLengthOfRepeatingSequence,
language))
{
}
}
}
Sample Usage
[PasswordStrength(minimumLength: 9,
minUppercase: 2,
minLowercase: 3,
minDigit: 2,
minSpecialCharacter: 2,
maxNoOfSameConsecutiveCharacters: 2,
maxNoOfConsecutiveAscendingDigits: MaxNoOfConsecutiveDigits.Three,
maxNoOfConsecutiveDescendingDigits: MaxNoOfConsecutiveDigits.Three,
maxNoOfConsecutiveAscendingCharacters: MaxNoOfConsecutiveCharacters.Three,
maxNoOfConsecutiveDescendingCharacters: MaxNoOfConsecutiveCharacters.Two,
minLengthOfRepeatingSequence: 2,
ErrorMessage = "Password must be at least 9 chars, 2 uppercase, 3 lowercase, 2 digit, 2 special char, no more than 2 same consecutive chars, no more than 3 consecutive ascending digits, no more than 3 consecutive descending digits, no more than 3 consecutive ascending characters, no more than 2 consecutive descending characters, no repeating sequence of more than 2 chars.")]
public string? Password { get; set; }
Multilingual feature
The validator supports below languages.
- English (default)
- Bangla
- Hindi
- Punjabi
- Chinese
- Korean
- Japanese
- Urdu
- Arabic
- Hebrew
You can set a property of the validator called Language.
For languages other than English, properties RequireLowercase & MinLowercase do not apply.
Below unit test demonstrates how this feature works:
[Theory]
[InlineData("তুমি কেমন আ1@1", Language.Bangla, true)] //Valid
[InlineData("তুমি 1@!", Language.Bangla, false)] //Invalid
[InlineData("मेरा पासवर्ड है1@1", Language.Hindi, true)] //Valid
[InlineData("मेरा1@1", Language.Hindi, false)] //Invalid
[InlineData("ਤੁਹਾਡਾ ਕੀ ਹਾਲ ਹੈ?1@1", Language.Punjabi, true)] //Valid
[InlineData("ਤੁਹਾ1@1", Language.Punjabi, false)] //Invalid
[InlineData("見到你很高興1@!", Language.Chinese, true)] //Valid
[InlineData("見到你很1@!", Language.Chinese, false)] //Invalid
[InlineData("어떻게 지내세요?1!", Language.Korean, true)] //Valid
[InlineData("어떻게 지?1!", Language.Korean, false)] //Invalid
[InlineData("おはようございます1@", Language.Japanese, true)] //Valid
[InlineData("おはよう1@!", Language.Japanese, false)] //Invalid
[InlineData("آپ سے مل کے اچھا لگا1@1", Language.Urdu, true)] //Valid
[InlineData("پ سے م1314", Language.Urdu, false)] //Invalid
[InlineData("صباح الخيرح1!1", Language.Arabic, true)] //Valid
[InlineData("صباح1!1", Language.Arabic, false)] //Invalid
[InlineData("1!נעים להכיר אות@אב1@", Language.Hebrew, true)] //Valid
[InlineData("אב1בא@3", Language.Hebrew, false)] //Invalid
public void MultilingualMinNoOfCharacters(string passwordToTest, Language language, bool expectedResult)
{
var validator = new PasswordStrengthValidator
{
MinimumLength = 6,
RequireDigit = false,
RequireSpecialCharacter = false,
RequireMaxNoOfSameConsecutiveCharacters = false,
RequireMaxNoOfConsecutiveAscendingDigits = false,
RequireMaxNoOfConsecutiveAscendingCharacters = false,
RequireMaxNoOfConsecutiveDescendingDigits= false,
RequireMaxNoOfConsecutiveDescendingCharacters= false,
RequireRepeatingSequenceCheck= false,
RequireLowercase = false,
RequireUppercase = true,
MinUppercase = 5,
Language = language
};
var result = validator.PasswordStrength(passwordToTest);
Assert.Equal(expectedResult, result);
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added multilingual feature.