简体   繁体   中英

ASP.NET Identity remove column from AspNetUsers table

When I use ASP.NET Identity first code approach, I want to generate columns in AspNetUsers table in my own way. I don't need to have stored multiple columns with null values. I just need columns Id, SecurityStamp and UserName. Only post, that I've found is here: AspNet Identity 2.0 Email and UserName duplication , but it is still unsloved (due to error in Santosh comment).

So can anybody tell my how to solve this?

EDIT: Is it even possible to delete some of these columns/properties?

Thanks

Actually you can ignore the fields, just you need to configure your entity OnModelCreating within your context Class as:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>().Ignore(c => c.AccessFailedCount)
                                           .Ignore(c=> c.LockoutEnabled)
                                           .Ignore(c=>c.LockoutEndDateUtc)
                                           .Ignore(c=>c.Roles)
                                           .Ignore(c=>c.TwoFactorEnabled);//and so on...

        modelBuilder.Entity<IdentityUser>().ToTable("Users");//to change the name of table.

}

Actually you can, just configure your entity on OnModelCreating of your context class.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityUser>().Ignore(u => u.AccessFailedCount);
    //and so on...
}

Or if your application has a separate file for each configuration (wich is what i recommend), you can do like this:

public class ApplicationUserEntityTypeConfiguration : EntityTypeConfiguration<ApplicationUser>
{
    public ApplicationUserEntityTypeConfiguration()
    {
        Ignore(p => p.AccessFailedCount);
        //And so on..
    }
}

The short answer is no, not without rolling your own implementation. Or you can wait for them to open source asp.net identity on codeplex . Who knows how long that will take.

The default implementation includes all of those unused columns (see below).

// Summary:
//     Default EntityFramework IUser implementation
//
// Type parameters:
//   TKey:
//
//   TLogin:
//
//   TRole:
//
//   TClaim:
public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>
    where TLogin : Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<TKey>
    where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<TKey>
    where TClaim : Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<TKey>
{
    // Summary:
    //     Constructor
    public IdentityUser();

    // Summary:
    //     Used to record failures for the purposes of lockout
    public virtual int AccessFailedCount { get; set; }
    //
    // Summary:
    //     Navigation property for user claims
    public virtual ICollection<TClaim> Claims { get; }
    //
    // Summary:
    //     Email
    public virtual string Email { get; set; }
    //
    // Summary:
    //     True if the email is confirmed, default is false
    public virtual bool EmailConfirmed { get; set; }
    //
    // Summary:
    //     User ID (Primary Key)
    public virtual TKey Id { get; set; }
    //
    // Summary:
    //     Is lockout enabled for this user
    public virtual bool LockoutEnabled { get; set; }
    //
    // Summary:
    //     DateTime in UTC when lockout ends, any time in the past is considered not
    //     locked out.
    public virtual DateTime? LockoutEndDateUtc { get; set; }
    //
    // Summary:
    //     Navigation property for user logins
    public virtual ICollection<TLogin> Logins { get; }
    //
    // Summary:
    //     The salted/hashed form of the user password
    public virtual string PasswordHash { get; set; }
    //
    // Summary:
    //     PhoneNumber for the user
    public virtual string PhoneNumber { get; set; }
    //
    // Summary:
    //     True if the phone number is confirmed, default is false
    public virtual bool PhoneNumberConfirmed { get; set; }
    //
    // Summary:
    //     Navigation property for user roles
    public virtual ICollection<TRole> Roles { get; }
    //
    // Summary:
    //     A random value that should change whenever a users credentials have changed
    //     (password changed, login removed)
    public virtual string SecurityStamp { get; set; }
    //
    // Summary:
    //     Is two factor enabled for the user
    public virtual bool TwoFactorEnabled { get; set; }
    //
    // Summary:
    //     User name
    public virtual string UserName { get; set; }
}

I know this might not be completely related, but if you simply want to exclude columns in JSON responses all you have to do is place [JsonIgnore] above the property. I use Entity so by default the (encrypted) password is included in the model. Even if the password is encrypted you still don't want the end-user to get it. One way to maintain access to that property without including it in a response is shown below.

In the below example the Password field would be removed from the Json response because we added [JsonIgnore] to the model.

public int Id { get; set; }
public string Email { get; set; }

[JsonIgnore]
public string Password { get; set; } // <--- Removed from JSON response

public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public bool Active { get; set; }

Here is a sample JSON response.

{
  "id": 1,
  "email": "ddavis@example.com",
  "firstName": "Daniel",
  "middleName": "Cool-Guy",
  "lastName": "Davis",
  "phoneNumber": "12055550000",
  "active": true
}

You can create descendant of IdentityUser override properties your need and decorate they [NotMapped] attribute. Then create (recreate) identity tables.

[NotMapped]
public override bool EmailConfirmed { get; set; }

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