5

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.

3
  • 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. Commented Nov 18, 2014 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. Commented Nov 18, 2014 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. Commented Nov 18, 2014 at 21:25

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.