4

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

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

But in the new version of asp.net identity, UserManager doesnt seem to have a property called UserValidator. The "UserValidator" is recognised but I suppose its added to the UserManager in a different way now. I cant see any relevant property on UserManager.

Edit:

The unit tests in the github repo for "Identity" has a test for this case. Its currently the last one in this file:

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

I guess this should give a clue as to the answer but I cant see where this would go 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);
        }
    }
rdans
  • 2,179
  • 22
  • 32

2 Answers2

5

In the ConfigureServices method of the Startup class, the AddIdentity method has an overload which allows different options to be configured.

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

Changing it to the below allows an email address to be used for usernames.

// Add Identity services to the services container.
services.AddIdentity<ApplicationUser, IdentityRole>(options => { options.User.UserNameValidationRegex = null; })
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();
rdans
  • 2,179
  • 22
  • 32
  • 3
    in asp core it is not regex : services.AddIdentity (options => { options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; }) – Michał W. Mar 10 '16 at 14:58
  • 1
    If that worked for you, you should add it as another answer. That way people can up-vote it if they find it helpful. – rdans Mar 11 '16 at 10:45
3

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

You can use the AddIdentity method's overload to set the options you want.

You would change this:

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

To this to allow email addresses to work:

// Add Identity services to the services container.
services.AddIdentity<ApplicationUser, IdentityRole>(options => { 
      options.User.AllowedUserNameCharacters =
                "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; 
     })
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();
Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133