Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.AspNetCore.Identity.UserManager:Warning: User validation failed: InvalidUserName;InvalidEmail

I need to create a user with UserName, FirstName and LastName and Email fields. I'm using Mysql database and Pomelo package. Migration and DB update are working fine.

This code results in failure.

public async Task<IActionResult> CreateUserAsync()
    {
        var user = new ApplicationUser(
            UserName: "blablabla",
            FirstName: "blablabla",
            LastName: "blablabla",
            Email: "[email protected]"
        );
        var successful = await mUserManager.CreateAsync(user
            , "Pasword123456#");
        if(successful.Succeeded)
            return Content("User was created", "text/html");

        return Content("User created FAILED", "text/html");
    }
}

The table in the database is empty

+-----------------------+ | Tables_in_calendario | +-----------------------+ | __efmigrationshistory | | aspnetroleclaims | | aspnetroles | | aspnetuserclaims | | aspnetuserlogins | | aspnetuserroles | | aspnetusers
| | aspnetusertokens | | events | | usuarios
| +-----------------------+ mysql> select * from aspnetusers; Empty set (0.00 sec)

I have no idea how to make it work.

In debug mode that is the only information I have. Debug mode

like image 291
Thadeu Antonio Ferreira Melo Avatar asked Oct 18 '25 05:10

Thadeu Antonio Ferreira Melo


2 Answers

I found out the problem.

I was overwriting Email and UserName in ApplicationUser. This was the warning in the building. enter image description here

The solution was simply removing the fields.

like image 50
Thadeu Antonio Ferreira Melo Avatar answered Oct 20 '25 20:10

Thadeu Antonio Ferreira Melo


In the Configure service You can do this where AppUser class is inherited from IdentityUser, you can use it directly also

services.AddIdentityCore<AppUser>(opt =>
{              
opt.Password.RequireDigit = false;
})
like image 28
Deep2359 Avatar answered Oct 20 '25 21:10

Deep2359