Personal Field Login to Identity

Asked

Viewed 244 times

0

Personal A doubt that however seemingly silly I did not find in any article that talks about Identity. How can I customize authentication features to use email as the user name instead of using another custom attribute as a name field "login" and combine it with the password already in the template?...

1 answer

0

When you add Identity: "services.Addidentity", you can pass your custom classes, for example, to the user:

services.AddIdentity<UsuarioCustomizado>();

Custom User must inherit the Identityuser class, so you add custom fields, in which case I added the CPF:

 public class ApplicationUser: IdentityUser
{
    public string CPF { get; set; }
}

Now for you to customize in the database as the keys, using EF Core, you can create a context by inheriting Identitydbcontext:

 public class ApplicationDbContext: IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
     : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.ApplyConfiguration(new ApplicationUserEntityTypeConfiguration());
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}

And finally in Onmodelcreating you can change the settings of the tables.

Reference: https://docs.microsoft.com/pt-br/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-2.2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.