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

I have a number of classes that I redefined 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, he says The UserName field is required. I tried to override the UserName field in my ApplicationUser class, which inherits from IdentityUser using [Index(IsUnique = false)] , however this just leads to a unique constraint and a unique index to create.

How to override the IdentityUser class to make the username null and not unique since I don't want to use it.

Note. I fully know that you can save the email in the username field using the AllowOnlyAlphanumericUserNames property in the UserValidator class. I really do not like this approach and simply would like to remove this restriction in order to save the record without a username. I will change any functions that I need that use the username to view the email address.

+6
source share
2 answers

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

+1
source

The only real way to get rid of it is to implement your own IUserStore . Which sounds like a lot of work with little to no practical benefit.

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

You also need to create your own implementation of IdentityDbContext or create the necessary tables / relations in ApplicationDbContext to remove the uniqueness check. Whic is also another load of work.

It just isn't worth it.

0
source

Source: https://habr.com/ru/post/975822/


All Articles