I have been working with .NET projects regularly, and I have always used the Identity package from .NET Core. Recently I had to work on a project where the main identifier is not a username, but the email. Because of this I wanted to find out how I can configure identity so that it does not use the username but I have not found anything that the package itself offers.
I would like to find a solution or configuration where I can remove the username field and have everything work correctly, or some kind of flag that tells the framework that the primary identifier is the email.
It seems strange to me that at this point the package does not support this natively, since the most common thing is that authentication is done with email and password.
I have been working with .NET projects regularly, and I have always used the Identity package from .NET Core. Recently I had to work on a project where the main identifier is not a username, but the email. Because of this I wanted to find out how I can configure identity so that it does not use the username but I have not found anything that the package itself offers.
I would like to find a solution or configuration where I can remove the username field and have everything work correctly, or some kind of flag that tells the framework that the primary identifier is the email.
It seems strange to me that at this point the package does not support this natively, since the most common thing is that authentication is done with email and password.
Share edited Mar 7 at 5:06 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 6 at 20:00 Isaac MoralesIsaac Morales 111 bronze badge1 Answer
Reset to default 2If you are using the default Identity UI package , it will automatically use the Email as the username by default.
There is no need to modify the Identity table to enable the username.
You could scaffold the register page to see how it works.
Codes:
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl ??= Url.Content("~/");
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
if (ModelState.IsValid)
{
var user = CreateUser();
//here it set the Email as the username
await _userStore.SetUserNameAsync(user, Input.Email, CancellationToken.None);
await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
Database: