Skip to content

VeritasSoftware/PasswordStrengthDataAnnotation

Repository files navigation

MyPasswordStrength

.NET Build & Test TypeScript Build & Test Angular Build & Test ReactJS Build & Test

Packages Version Downloads
MyPasswordStrength Nuget Version Downloads count
ts-my-password-strength NPM Version Downloads count
angular-my-password-strength NPM Version Downloads count
react-my-password-strength NPM Version Downloads count

Typescript Library Angular Library ReactJS Library .NET MAUI Library

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.

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 = 8,
    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.Three
};

var password = "P@76w0rD12!";

// 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)
            : base(PasswordStrengthValidator.GetRegexPattern(minimumLength, requireUppercase, minUppercase, requireLowercase, minLowercase,
                                                                requireDigit, minDigit, requireSpecialCharacter, minSpecialCharacter, specialCharacters,
                                                                requireMaxNoOfSameConsecutiveCharacters, maxNoOfSameConsecutiveCharacters,
                                                                requireMaxNoOfConsecutiveAscendingDigits, maxNoOfConsecutiveAscendingDigits,
                                                                requireMaxNoOfConsecutiveDescendingDigits, maxNoOfConsecutiveDescendingDigits))
        {
        }
    }
}

Sample Usage

[PasswordStrength(minimumLength: 9,
				  minUppercase: 2,
				  minLowercase: 3,
				  minDigit: 2,
				  minSpecialCharacter: 2,
				  maxNoOfSameConsecutiveCharacters: 2,
                  maxNoOfConsecutiveAscendingDigits: MaxNoOfConsecutiveDigits.Three,
                  maxNoOfConsecutiveDescendingDigits: MaxNoOfConsecutiveDigits.Three,
				  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.")]
public string? Password { get; set; }

About

A password strength data annnotation for asp net core.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors