I would like to extend UserValidator or something similar in Asp.net Identity 2.0 to not only check for unique email but also a unique value of my choosing. Example of what I would like to do with Alias below. Is this possible or do I have to write a check everywhere I can update Alias?
public class ApplicationUserManager : UserManager<ApplicationUser, int>
{
public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
: base(store)
{
this.UserValidator = new UserValidator<ApplicationUser, int>(this)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
//This does not exist
//RequireUniqueAlias = true
};
}
}
public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole,
CustomUserClaim>
{
[Required]
public string Alias { get; set; }
}
A bit tricky to do but possible, needs a custom UserValidator. Looking at Microsoft.AspNet.Identity.Core source really helped me.
https://aspnetidentity.codeplex.com/SourceControl/latest#src/Microsoft.AspNet.Identity.Core/UserValidator.cs
CustomUserValidator, removed some comments due to body character limitation:
Apart from small modifications you also need to add a custom resource file, this is really useful as well if you wish to customize Asp.net Identity 2.0 username already taken validation message or other messages. Add a resource file, for example CustomResources.resx. Open folder in file explorer and edit CustomResource.resx in notepad or similar. Replace with the following data:
Open CustomResources.resx and add your property, in my case "DuplicateAlias" as name. Now open CustomResource.Designer and add the following methods below
internal static global::System.Globalization.CultureInfo Culture