C.J.
C.J.

Reputation: 3527

ASP.NET Identity: Make other fields in AspNetUsers unique key

I'm working on the ASP.NET Identity. By default, the AspNetUsers table comes with a few columns such as: ID, UserName, HashPassword, Email, EmailConfirmed, Phone etc.

By default, the ID column is the Primary Key and UserName has the Unique Constraint.

How do I make other fields such as Email and/or Phone to have the same unique constraint as the UserName?

What I have done so far:

I manage to add the unique constraint to the database manually, however, unlike the UserName field, I was unable to do the validation on the application level. For example: The UserName field will display the error message "Name XXX is already taken" if the user tries to register an account with the same username.

Upvotes: 4

Views: 2677

Answers (1)

Hao Kung
Hao Kung

Reputation: 28200

To enforce uniqueness at the UserManager layer, you can implement your own IUserValidator and check for your custom uniqueness rules

Upvotes: 1

Related Questions