Search code examples
asp.net-identity-3

Error with German character in Asp.Net Core Identity


When I try to create a User with the UserManager and there are characters like ä, ö, ü in the UserName, I get a succeeded=false. I don’t understand why it is a problem. The DB is nvarchar (255) and should not have a problem with German characters. I can change it afterword by using the DbContext. How can I change this behavior?


Solution

  • Usermanager validates the username by checking if there is any character not contained in the list of allowed characters. You can change that list:

    services.Configure<IdentityOptions>(options =>
    {
         // The list of allowed characters in the username used to validate user names 
         // you can add German characters here
         options.User.AllowedUserNameCharacters 
            = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
    });
    
    services.AddIdentity<ApplicationUser, IdentityRole>()
           .AddEntityFrameworkStores<ApplicationDbContext>()
           .AddDefaultTokenProviders();