Seeding users with UserName equal to Email

I’ve got a database project I’m using for migrations, have setup the AppHost properly to use my CustomAuthRepository (using an existing user store), but when seeding some initial users using CustomAuthRepository (which has no override for CreateUserAuth, it is using the BaseAuthRepository by default), it does not work because of issues with the UserName:

If I don’t populate a username, it’s a failure at the DB level (because UserName is required)
When I populate a UserName (which are emails), it fails with:

UserName contains invalid characters
Parameter name: UserName

I’m assuming because of the @ symbol. I have not seen a way to override the UserName validation, or to use emails by default.

To create a User with an Email UserName use the Email field and leave the UserName unspecified. Users will still be able to login using their Email which will check against the Email field.

It seems this could be an incompatibility with my legacy users schema, as it is most likely the one that requires a UserName (it duplicates the UserName as the Email field). Is there no path forward other than making it nullable?

The Email field needs to be populated but you can relax the UserName validation when registering the AuthFeature plugin, e.g:

Plugins.Add(new AuthFeature(...) {
  ValidUserNameRegEx = new Regex(@"^(?=.{3,20}$)([A-Za-z0-9][._-]?)*$$", RegexOptions.Compiled)

Will this work at the AuthRepository level?

There’s no additional Username validation in the Auth Repo, but the behavior still remains if the UserName contains a @ then it will match on the Email field, otherwise it will match on the UserName field when validating UserName/Password.

1 Like

Thank you, this fixed my issue!

1 Like