Kavi Arshan
Kavi Arshan

Reputation: 83

How to remove UserName field from asp.net core 3 Identity

I wanna use phoneNumber field as a unique field and remove UserName Is it possible?

Upvotes: 0

Views: 1928

Answers (2)

Hector Huerta
Hector Huerta

Reputation: 1

This my user class, inherit from IdentityUser

public class User : IdentityUser<long>
{
    public DateTime CreatedAt { get; set; }
    public string FirstName { get; set; }
}

Here i remove a few properties

public class UserConfiguration : IEntityTypeConfiguration<User>
{
    public void Configure(EntityTypeBuilder<User> builder)
    {
        builder.Ignore(x => x.UserName);
        builder.Ignore(x => x.NormalizedUserName);
        builder.Ignore(x => x.LockoutEnabled);
        builder.Ignore(x => x.LockoutEnd);
        builder.Ignore(x => x.TwoFactorEnabled);
    }
}

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239270

No. You cannot modify the default IdentityUser model. However, you can treat the phone number as the user name, as using email for user name works, i.e. you simply just make it the value of UserName as well. For example, when using email as username, both Email and UserName hold the email address. Simply do the same with your phone number. Make the phone number the value of UserName, and then you're good to go.

Upvotes: 3

Related Questions