Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Email as Username in ASP.NET Core Identity

I am creating a User using ASP.NET Core Identity as follows:

User user = new User {
  Email = "[email protected]",
  Name = "John"
};            

await manager.CreateAsync(user, "JohnPass");

I get an error saying the Username is invalid because it is null.

How to configure Identity to use the Email as Username?

Or do I need to manually set the Username equal to the Email?

like image 875
Miguel Moura Avatar asked Oct 18 '22 00:10

Miguel Moura


1 Answers

Use below code based on mandatory items

User user = new User {
  Id = id,// some auto generated id
  Email = "[email protected]",
  UserName = "[email protected]",// because you want to keep email id as UserName
  DisplayName= "John"
};            

await manager.CreateAsync(user, "JohnPass");

One potential issue with this approach is that if we keep email as 'User Name', then in some cases, it may not allow the user to change their email id in the future.

like image 172
GauravKP Avatar answered Oct 21 '22 16:10

GauravKP