How to remove username validation ASP.NET MVC CORE?

I'm trying to change the user's password in this way:

await _userManager.ResetPasswordAsync(user, code, editViewModel.Password);
  • user - object ApplicationUser,
  • code - password reset code generated by the function _userManager.GeneratePasswordResetTokenAsync,
  • editViewModel.Password - a string containing the new password.

When trying to change the password, the result is an error User name 'Дмитрий' is invalid, can only contain letters or digits. (Dmitry is the user name). How do I cancel user name validation when I change my password?

Author: Pavel Mayorov, 2017-05-16

1 answers

The allowed characters in the user name are set in UserOptions.AllowedUserNameCharacters. Here is the default value:

/// <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; } = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";

Judging by the code, the check can be disabled by resetting this line to empty or to null:

else if (!string.IsNullOrEmpty(manager.Options.User.AllowedUserNameCharacters) &&
    userName.Any(c => !manager.Options.User.AllowedUserNameCharacters.Contains(c)))
{
    errors.Add(Describer.InvalidUserName(userName));
}

You can reset this setting in the ConfigureServices method:

services.AddIdentity<ApplicationUser, IdentityRole>(opts =>
{
    opts.User.AllowedUserNameCharacters = null;
}
 3
Author: PashaPash, 2018-06-13 05:16:27