Asp.net Identity doesn't allow same username for two or more users even if the primary key in AspNetUsers table is the Id, so i tried to ovveride the UserValidator of the UserManager but it stills give me errors that i can't give two users the same username , here is the custom validator:
public class CustomUserValidator<TUser> : IIdentityValidator<TUser>
where TUser : ApplicationUser, Microsoft.AspNet.Identity.IUser
{
private readonly UserManager<TUser> _manager;
public CustomUserValidator()
{
}
public CustomUserValidator(UserManager<TUser> manager)
{
_manager = manager;
}
public async Task<IdentityResult> ValidateAsync(TUser item)
{
var errors = new List<string>();
if (_manager != null)
{
var otherAccount = await _manager.FindByNameAsync(item.UserName);
if (otherAccount != null && otherAccount.CodeClient == item.CodeClient)
errors.Add("Utilisateur existant.");
}
return errors.Any()
? IdentityResult.Failed(errors.ToArray())
: IdentityResult.Success;
}
}
and in the constructor of my controller i change the uservalidator property like this :
public UsersRootController(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{
UserManager = userManager;
RoleManager = RoleManager;
UserManager.UserValidator = new CustomUserValidator<ApplicationUser>(userManager);
}
When i try to call the createasync method :
var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email, Nom = model.Nom, Prenom = model.Prenom, IsEnabled = model.IsEnabled, CodeClient = model.Client };
resultUser = await UserManager.CreateAsync(user, model.Password);
It uses my custom validator but returns always false .
Is there any way to fix that ?