Search code examples
asp.net-identityuniqueowin

AspNet Identity RequireUniqueEmail = false throws exception on CreateAsync


I initialize UserValidator of the UserManager with RequireUniqueEmail=false, the validator accepts duplicate emails for sure (tried to inherit it and override the ValidateAsync() method). However, when I try to create another user with a unique UserName but with an Email that already exists in the AspNetUsers table, CreateAsync() throws

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

There is just one validation error:

Email [email protected] is already taken.

I've checked the properties of the UserValidator before CreateAsync and RequireUniqueEmail=false, so it's initialized correctly. Funny thing is when I set RequireUniqueEmail to true it won't throw an exception, only return IdentityResult with the same error message in it.

Owin v.3.1.0 is used (same with v.3.0.1)


Solution

  • After a long and deep research and debugging I've found that you not only have RequireUniqueEmail flag in your UserValidator, but also same flag in your IdentityDbContext<ApplicationUser>, and they both should have the same value.