2

I am using asp.net Identity for authorization and authentication. I tried creating a user name with Hebrew letters but it didn't work.

With the old Membership I was able to use Hebrew characters for user names. Is it possible with Identity to use non-Latin characters?

Dov Miller
  • 1,958
  • 5
  • 34
  • 46

2 Answers2

2

I think you can attempt to do something like this:

Create a new Custom user validator to do your own custom validation( You can use a regex as well for validations):

public class CustomUserValidator<TUser> : IIdentityValidator<TUser>
    where TUser : class, Microsoft.AspNet.Identity.IUser
{
    public CustomUserValidator(UserManager<TUser> manager)
    {
        _manager = manager;
    }

    public async Task<IdentityResult> ValidateAsync(TUser item)
    {
        // Your custom validation here
    }
}

and then when you are creating your application user manager you can set the Default user validator to an instance of this new validator class:

manager.UserValidator = new CustomUserValidator<ApplicationUser>(manager)
{
    AllowOnlyAlphanumericUserNames = false,
    RequireUniqueEmail = true
};

[NOTE:] default validator is like:

manager.UserValidator = new UserValidator<ApplicationUser>(manager)
    {
        AllowOnlyAlphanumericUserNames = false,
    };
Jinish
  • 1,983
  • 1
  • 17
  • 22
  • Is it possible to set AllowOnlyAlphanumericUserNames to false without creating a Custom User Validator? – Dov Miller Mar 20 '17 at 10:59
  • 1
    yes absolutely. You can do that with the default user validator as well. Modified the answer. – Jinish Mar 20 '17 at 11:00
  • Thank you! You got me in the right direction. I'm using my own UserManager and put the code in the Constructor like this: this.UserValidator = new UserValidator(this) { AllowOnlyAlphanumericUserNames = false, };. This is also described here: http://stackoverflow.com/questions/19459591/allowonlyalphanumericusernames-how-to-set-it-rc-to-rtm-breaking-change-asp. Again thank you very much! – Dov Miller Mar 20 '17 at 11:13
1

in Program.cs (or Startup.cs) add options.User.AllowedUserNameCharacters:

builder.Services.AddIdentity<ApplicationUser, ApplicationRole>(
        options =>
        {
            options.SignIn.RequireConfirmedAccount = true;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.User.AllowedUserNameCharacters = "אבגדהוזחטיכלמנסעפצקרשתךםןףץabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
        }

yor can also use:

options.User.AllowedUserNameCharacters = String.Empty;

to allow all characters