4

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.

C.J.
  • 3,409
  • 8
  • 34
  • 51
  • What are you trying to do exactly? Make sure both username and email are unique? Why do you care if email is unique unless you're using it to look up the user, in which case make the username their email and you have a unique already built in. – Erik Funkenbusch Nov 18 '14 at 21:20
  • users can have the same telephone number. And I've seen some IT companies use the same email address for multiple workers. Think hard before setting such fields to unique. – StarPilot Nov 18 '14 at 21:21
  • The requirement is that one email address can only be tied to one user name. I tried to make the "username" to become "email" but it is not going to work either because I don't know how to add custom validation to the built in AspNetUsers properties. – C.J. Nov 18 '14 at 21:25

1 Answers1

1

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

Hao Kung
  • 28,040
  • 6
  • 84
  • 93