Added strong password validation

This commit is contained in:
2025-06-16 22:43:02 +02:00
parent a4b8458542
commit 14d9b45413
5 changed files with 149 additions and 14 deletions

View File

@@ -0,0 +1,10 @@
namespace BasicDotnetTemplate.MainProject.Enum;
public static class PasswordValidationEnum
{
public const string MIN_LENGTH = "Password must be at least 8 characters long";
public const string MIN_UPPER = "Password must have at least 2 uppercase letters";
public const string MIN_LOWER = "Password must have at least 2 lowercase letters";
public const string MIN_NUMBER = "Password must be at least 2 numbers";
public const string MIN_SPECIAL = "Password must be at least 2 special characters";
public const string IDENTICAL_CHARS = "Password cannot have 3 or more consecutive identical characters";
}

View File

@@ -30,21 +30,21 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
<PackageReference Include="Microsoft.Identity.Web" Version="3.9.2" />
<PackageReference Include="Microsoft.Identity.Web" Version="3.9.3" />
<PackageReference Include="MongoDB.Driver" Version="3.4.0" />
<PackageReference Include="MongoDB.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.4.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.4.0" />
<PackageReference Include="NLog" Version="5.5.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.5.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.2" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="8.1.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.4" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="8.1.4" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="8.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters.Abstractions" Version="8.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="8.1.2" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="8.1.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="8.1.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="8.1.2" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="8.1.4" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="8.1.4" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="8.1.4" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="8.1.4" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,60 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using BasicDotnetTemplate.MainProject.Enum;
using BasicDotnetTemplate.MainProject.Models.Settings;
namespace BasicDotnetTemplate.MainProject.Utils;
public partial class PasswordUtils
{
private const int MIN_LENGTH = 8;
private const int MIN_UPPER = 2;
private const int MIN_LOWER = 2;
private const int MIN_NUMBER = 2;
private const int MIN_SPECIAL = 2;
[GeneratedRegex("[A-Z]")]
private static partial Regex RegexUpper();
[GeneratedRegex("[a-z]")]
private static partial Regex RegexLower();
[GeneratedRegex("[0-9]")]
private static partial Regex RegexNumber();
[GeneratedRegex("[^a-zA-Z0-9]")]
private static partial Regex RegexSpecial();
[GeneratedRegex(@"(\S)\1{2,}", RegexOptions.IgnoreCase | RegexOptions.Compiled)]
private static partial Regex RegexIdenticalChars();
public static List<string> ValidatePassword(string password)
{
List<string> errors = [];
if (password.Length < 8)
errors.Add(PasswordValidationEnum.MIN_LENGTH);
if (RegexUpper().Matches(password).Count < MIN_UPPER)
errors.Add(PasswordValidationEnum.MIN_UPPER);
if (RegexLower().Matches(password).Count < MIN_LOWER)
errors.Add(PasswordValidationEnum.MIN_LOWER);
if (RegexNumber().Matches(password).Count < MIN_NUMBER)
errors.Add(PasswordValidationEnum.MIN_NUMBER);
if (RegexSpecial().Matches(password).Count < MIN_SPECIAL)
errors.Add(PasswordValidationEnum.MIN_SPECIAL);
if (RegexIdenticalChars().IsMatch(password))
errors.Add(PasswordValidationEnum.IDENTICAL_CHARS);
return errors;
}
}