UserValidator on Microsoft.AspNet.Identity vnext

I have a problem when I cannot use email addresses as usernames when using microsoft.aspnet.identity (individual user accounts selected when creating a new project - default mvc project template for asp.net 5). I read in many places that this solution:

UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager) { AllowOnlyAlphanumericUserNames = false }; 

But in the new version of the asp.net ID, the UserManager does not seem to have the UserValidator property. The "UserValidator" is recognized, but I suppose it is added to the UserManager differently. I cannot see any corresponding property in the UserManager.

Edit:

In unit tests in the github registry for "Identity" there is a test for this case. He is currently the last in this file:

https://github.com/aspnet/Identity/blob/dev/test/Microsoft.AspNet.Identity.Test/UserValidatorTest.cs

I suppose this should give a clue to the answer, but I don't see how it will look in my code.

  [Theory] [InlineData(" test_email@foo.com ", true)] [InlineData("hao", true)] [InlineData("test123", true)] [InlineData("!noway", true)] [InlineData(" foo@boz #.com", true)] public async Task CanAllowNonAlphaNumericUserName(string userName, bool expectSuccess) { // Setup var manager = MockHelpers.TestUserManager(new NoopUserStore()); manager.Options.User.UserNameValidationRegex = null; var validator = new UserValidator<TestUser>(); var user = new TestUser {UserName = userName}; // Act var result = await validator.ValidateAsync(manager, user); // Assert if (expectSuccess) { IdentityResultAssert.IsSuccess(result); } else { IdentityResultAssert.IsFailure(result); } } 
+4
asp.net-core visual-studio-2015 asp.net-core-mvc asp.net-identity-3
source share
2 answers

In the ConfigureServices method of the Startup class, the AddIdentity method has an overload that allows you to configure various parameters.

 // Add Identity services to the services container. services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); 

Changing it to the following allows you to use the email address for usernames.

 // Add Identity services to the services container. services.AddIdentity<ApplicationUser, IdentityRole>(options => { options.User.UserNameValidationRegex = null; }) .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); 
+5
source share

As Michal W. mentioned, you can configure this in the ConfigureServices method of the Startup class.

You can use the overload of the AddIdentity method to set the necessary parameters.

You would change this:

 // Add Identity services to the services container. services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); 

To do this, to enable email addresses to work:

 // Add Identity services to the services container. services.AddIdentity<ApplicationUser, IdentityRole>(options => { options.User.AllowedUserNameCharacters = "ab cdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@ +"; }) .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); 
+3
source share

All Articles