Username and Email Policy in ASP.NET Identity
In this article, I am going to discuss Username and Email Policy in ASP.NET Identity and How to Customize them. Please read our previous article where we discussed How to Create Custom Password Policy in ASP.NET Identity.
Username and Email Policy in ASP.NET Identity:
ASP.NET Identity also implements some default policies for usernames and email addresses. For example, we have stored a user with an email@example.com email address.
Now, if we try to register another user with the same firstname.lastname@example.org email address, then we will get an error message as shown in the below image.
Where is this Email Validation Policy Defined in ASP.NET Identity?
This Email Uniqueness Policy is defined inside the IdentityConfig.cs file. If you open the IdentityConfig.cs file, then you will see the Create method of the ApplicationUserManager class and inside this method, the following logic is implemented for Email and Username validation.
The meaning of the above two properties is as follows:
- AllowOnlyAlphanumericUserNames: Allow only letters or digits in the usernames if this property is set to true else it can allow anything.
- RequireUniqueEmail: Require a unique email address while creating a new user if this property is set to true.
Now, if you want to define your own validation logic for usernames and emails, go to App_Start > IdentityConfig.cs file and then go to the Create method of the ApplicationUserManager class and override the values of the above properties.
For example, if you only want to allow alphanumeric as the username means you don’t want to allow any special symbols as part of the username, then you need to set the value of the AllowOnlyAlphanumericUserNames property to true as follows.
With the above AllowOnlyAlphanumericUserNames configuration whose value is set to true, now, if we try to add a user with a username containing special characters, we will get an error message as shown in the below image.
In the next article, I am going to discuss Login a User in ASP.NET Identity. Here, in this article, I try to explain Username and Email Policy in ASP.NET Identity. I hope you enjoy this Username and Email Policy in the ASP.NET Identity article.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.