ASP.NET标识从AspNetUsers表中删除列

当我使用ASP.NET Identity第一代码方法时,我想以自己的方式在AspNetUsers表中生成列。 我不需要存储多个具有空值的列。 我只需要列Id,SecurityStamp和UserName。 我发现只有post在这里: AspNet Identity 2.0电子邮件和用户名重复 ,但它仍然没有被发现(由于Santosh评论中的错误)。

所以有人能告诉我如何解决这个问题吗?

编辑:甚至可以删除其中一些列/属性?

谢谢

简短的回答是否定的,不是没有滚动你自己的实现。 或者你可以等待他们在codeplex上开源asp.net身份。 谁知道需要多长时间。

默认实现包括所有未使用的列(见下文)。

 // Summary: // Default EntityFramework IUser implementation // // Type parameters: // TKey: // // TLogin: // // TRole: // // TClaim: public class IdentityUser : IUser where TLogin : Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole where TClaim : Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim { // 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 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 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 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; } } 

实际上你可以忽略这些字段,只需要在上下文类中配置你的实体OnModelCreating

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity().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().ToTable("Users");//to change the name of table. } 

实际上,您可以在上下文类的OnModelCreating上配置您的实体。

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

或者,如果您的应用程序为每个配置都有一个单独的文件(我推荐的那样),您可以这样做:

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

我知道这可能不是完全相关的,但是如果你只想在JSON响应中排除列,那么你所要做的就是将[JsonIgnore]置于属性之上。 我使用Entity,因此默认情况下(加密)密码包含在模型中。 即使密码已加密,您仍然不希望最终用户获得密码。 下面显示了一种维护对该属性的访问而不将其包含在响应中的方法。

在下面的示例中, 密码字段将从Json响应中删除,因为我们将[JsonIgnore]添加到模型中。

 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; } 

以下是JSON响应示例。

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