.NET MVC 6 / vNext UserValidator for displaying alphanumeric characters

What I'm trying to do is just allow alphanumeric characters during the .NET MVC 6 / vNext registration process and every lesson I've seen (i.e. Set up Microsoft.AspNet.Identity to specify an email address as username ) UserManager.UserValidator , which is now not available in the current structure.

I saw this: UserValidator in Microsoft.AspNet.Identity vnext , which looks just like mine, but they took the UserNameValidationRegex property from the frame since then. Brilliant.

 services.AddIdentity<ApplicationUser, IdentityRole>( o => { o.Password.RequireDigit = false; o.Password.RequireLowercase = false; o.Password.RequireUppercase = false; o.Password.RequireNonLetterOrDigit = false; o.Password.RequiredLength = 2; //o.User.AllowedUserNameCharacters here seems to be the only logical thing I can access }) .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); 

I found o.User.AllowedUserNameCharacters , but in the absence of the documentation I found on this, I have no idea in what format I should install this? Anyone out there in the same boat coping with this?

Thanks in advance.

+7
c # asp.net-mvc asp.net-core
source share
3 answers

I looked at the aspnet / Identity GitHub repository and searched for the property you found ( AllowedUserNameCharacters ). Here is what I have.

From UserOptions.cs class to repo: [Link here]

 /// <summary> /// Gets or sets the list of allowed characters in the username used to validate user names. /// </summary> /// <value> /// The list of allowed characters in the username used to validate user names. /// </value> public string AllowedUserNameCharacters { get; set; } = "ab cdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@ +"; 

So, you will need to set this property as "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" (that is, remove characters from the end of the originally set string). This should achieve what you want.

In terms of why this change was made, according to issue number 558, they are trying to move away from regular expressions to increase security. Look here for full details.

+12
source share

As @DoubleVoid noted in the comments, you can use an empty string to allow any characters.

+1
source share

If you need a special character in your letter. Try the code below

  services.Configure<IdentityOptions>(options = { options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'*+-/=?^_`{|} ~.@ "; }); 

Valid email address https://en.wikipedia.org/wiki/Email_address#Local-part

0
source share

All Articles