ApplicationUser的继承

时间:2015-10-22 00:30:59

标签: asp.net-mvc inheritance

我正在尝试正确设置我的类并学习如何正确使用继承。使用MVC 5,实体框架和身份模型来做到这一点。我有两个用户,我想从不同的页面(商家和粉丝)注册。我想固有ApplicationUser,但对继承如何工作感到困惑,因为ApplicationUser继承自IdentityUser。

我希望能够访问IdentityUser中的email和userName字段以进行注册。那么在创建我的Merchant和Follower类时,我是继承AppUser还是IdentityUser?

App用户类:

    namespace IdentitySample.Models

{

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    static ApplicationDbContext()
    {
        // Set the database intializer which is run once during application start
        // This seeds the database with admin user credentials and admin role
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}
    }

身份等级:

    namespace Microsoft.AspNet.Identity.EntityFramework
    {
// 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; }
}
    }

1 个答案:

答案 0 :(得分:0)

我认为你应该简单地从ApplicationUser类继承,所以你将拥有以下内容:

h2

现在,Merchant和Follower都继承了IdentityUser和ApplicationUser的所有属性。