简体   繁体   中英

ASP.NET Core - How to customize some fields in IdentityDbContext

In ASP.NET Core IdentityDbContext, I am using EF Code First. I have:

public class DDMDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, long, IdentityUserClaim<long>, ApplicationUserRole, IdentityUserLogin<long>, IdentityRoleClaim<long>, IdentityUserToken<long>>
{
    public DDMDbContext(DbContextOptions<DDMDbContext> options)
    : base(options) { }

    public virtual DbSet<ApplicationRole> ApplicationRole { get; set; }
    public virtual DbSet<ApplicationUserRole> ApplicationUserRole { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<ApplicationUserRole>(userRole =>
        {
            userRole.HasKey(ur => new { ur.UserId, ur.RoleId });

            userRole.HasOne(ur => ur.Role)
                .WithMany(r => r.UserRoles)
                .HasForeignKey(ur => ur.RoleId)
                .IsRequired();

            userRole.HasOne(ur => ur.User)
                .WithMany(r => r.UserRoles)
                .HasForeignKey(ur => ur.UserId)
                .IsRequired();
        });

    }
}

namespace DDM.API.Core.Data.Identity
{
    public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public ICollection<ApplicationUserRole> UserRoles { get; set; }
    }

    public class ApplicationRole : IdentityRole
    {
        public ICollection<ApplicationUserRole> UserRoles { get; set; }
    }

    public class ApplicationUserRole : IdentityUserRole<long>
    {
        public virtual ApplicationUser User { get; set; }
        public virtual ApplicationRole Role { get; set; }
    }
}

I found that in ApplicationUser, these fields are required.:

EmailConfirmed
PhoneNumberConfirmed
TwoFactorEnabled
LockoutEnabled
AccessFailedCount

I tried to use:

            entity.Property(m => m.EmailConfirmed).IsRequired(false);
            entity.Property(m => m.PhoneNumberConfirmed).IsRequired(false);
            entity.Property(m => m.TwoFactorEnabled).IsRequired(false);
            entity.Property(m => m.LockoutEnabled).IsRequired(false);
            entity.Property(m => m.AccessFailedCount).IsRequired(false);

But got error:

The property 'ApplicationUser.EmailConfirmed' cannot be marked as nullable/optional because the type of the property is 'bool' which is not a nullable type. Any property can be marked as non-nullable/required, but only properties of nullable types can be marked as nullable/optional.

How do I change these fields to Not Required ?

Thanks

EmailConfirmed
PhoneNumberConfirmed
TwoFactorEnabled
LockoutEnabled
AccessFailedCount

These properties was hardcoded at the root level of IdentityRole<> in.Net Identity, with type bool , not bool? so it cannot be optional even if we configure through fluent API.

How do I change these fields to Not Required?

I don't get it so far... If we doesn't need it, why not simply ignore it? Like we usually do with userRole.Ignore(x => x.EmailConfirmed); . But override it to become optional doesn't make so much sense.

Just a caveat, UserManager , LoginManager and a few more manager which ever make use of those property would get their default value (for example, bool property ignored always have false value), then that might cause some unexpected behavior afterward.

You should leave those properties as they are, just ignore them for sign in, lockout, etc. Check this article: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-configuration?view=aspnetcore-6.0 So, for Sign-In you could use:

services.Configure<IdentityOptions>(options =>
{
    // Default SignIn settings.
    options.SignIn.RequireConfirmedEmail = false;
    options.SignIn.RequireConfirmedPhoneNumber = false;
});

And rest of properties could be ignored by similar logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM