Back to: ASP.NET MVC Tutorial For Beginners and Professionals
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 info@dotnettutorials.net email address.
Now, if we try to register another user with the same info@dotnettutorials.net 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.