Connel
Connel

Reputation: 1881

Remove unique constrant and make username nullable in ASP.NET Identity

I have a series of classes I have overridden for use in ASP.NET Identity 2.0 so that they use the email address field rather than the email field.

When trying to create a user it says The UserName field is required. I have tried overriding the UserName field in my ApplicationUser class which inherits from IdentityUser with [Index(IsUnique = false)] however this just results in a unique constraint and a non-unique index being created.

How can I override the IdentityUser class to make the username nullable and non-unique as I don't want to use it.

Note: I'm fully aware that you can store the email in the username field using the AllowOnlyAlphanumericUserNames property in the UserValidator class. I really don't like this approach and would just like to remove this constraint so I can store the record without a username. I will modify any functions that I need that use the username to look at the email address.

Upvotes: 6

Views: 3879

Answers (3)

Azerue
Azerue

Reputation: 368

I found the best solution just clear the user validator before using user manager and write your own logic. No need to override any class.

UserManager<ApplicationUser> userManager,

    userManager.UserValidators.Clear();

Upvotes: 0

Konstantin Tarkus
Konstantin Tarkus

Reputation: 38428

Here is the full implementation of the IUserStore - http://git.io/XZ3psA I wouldn't say that there is a lot of work.

Upvotes: 1

trailmax
trailmax

Reputation: 35126

The only real way to get rid of it is to implement your own IUserStore. Which sounds like a lot of work for almost no practical gain.

See UserManager has method FindByNameAsync() and IUserStore<TUser, in TKey> also requires method Task<TUser> FindByNameAsync(string userName) to be present on the implementing class.

Also you'll need to create your own implementation of IdentityDbContext, or create required tables/relationships in your ApplicationDbContext to remove the uniqueness validation. Whic is also another load of work.

It is just not worth it.

Upvotes: 0

Related Questions