Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for default ASP.NET Core Identity Password

Note: This question, I believe, is not the duplicate of this question. My question is dealing with the default validation rules asp.net core identity has for password validation and how it's regex can be made, while the linked question is discussing, in general about act of validating password (which doesn't solve my problem)

The ASP.NET Core enables default following password validation

  1. Minimum 8 characters
  2. Should have at least one number
  3. Should have at least one upper case
  4. Should have at least one lower case
  5. Should have at least one special character (Which special characters are allowed?)

Keeping these conditions in mind I tried making the following regex but it is not working.

^((?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])|(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[^a-zA-Z0-9])|(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[^a-zA-Z0-9])|(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^a-zA-Z0-9])).{8,}$

This regex is accepting the strings even when either of three conditions from points 2,3,4,5 matches. But I want that all conditions should satisfy.

What am I doing wrong here?

like image 636
Karan Desai Avatar asked Feb 06 '18 03:02

Karan Desai


People also ask

What is Aspnet core identity?

ASP.NET Core Identity: Is an API that supports user interface (UI) login functionality. Manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.

Which validation is used for password in asp net?

In ASP.NET you can use the RegularExpressionValidator control to enforce the password policy.


1 Answers

so, use

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$^+=!*()@%&]).{8,}$
  • ^: first line
  • (?=.*[a-z]) : Should have at least one lower case
  • (?=.*[A-Z]) : Should have at least one upper case
  • (?=.*\d) : Should have at least one number
  • (?=.*[#$^+=!*()@%&] ) : Should have at least one special character
  • .{8,} : Minimum 8 characters
  • $ : end line

for more information: this

like image 121
Mohammad Javad Noori Avatar answered Sep 22 '22 15:09

Mohammad Javad Noori