How to force Asp.Net Identity 2.0 to use custom Ro

2019-07-03 22:51发布

问题:

I have extended the IdentityUserRole which is as follow

public class ApplicationUserRoles : IdentityUserRole<string>
{
    [Key]
    public string ApplicationId { get; set; }

    public virtual AspNetApplications AspNetApplications { get; set; }
}

and my AspNetApplications Class is as follow

public class AspNetApplications
{
    [Key]
    public string ApplicationId { get; set; }
    public string ApplicationName { get; set; }
}

Migration has created the AspNetApplications and ApplicationUserRoles tables in DB. A screen shot is as follow.

Following is my Identity Model

public class ApplicationUser : IdentityUser
{
    public virtual AspNetApplications AspNetApplication { get; set; }
    public virtual ApplicationUserRoles AspNetUserRoles { get; set; }
    //public virtual ICollection<AspNetApplicationUsers> AspNetApplicationUsers { get; set; }

    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)
    {
    }

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

    public DbSet<AspNetApplications> AspNetApplications { get; set; }
    public DbSet<ApplicationUserRoles> AspNetUserRoles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ApplicationUserRoles>().HasKey(m => new { m.ApplicationId, m.UserId, m.RoleId });
    }

}

everything is good so far but now when I inspect the User in my Account Controller, it is still bringing Role based information from AspNetUserRoles. Could please anyone tell me how I can make use of my custom ApplicationUserRoles instead of IdentityUserRole.

回答1:

If you look at the signature of IdentityUser (your ApplicationUser inherits from it)

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>
{

}

you can see that it accepts your custom definition of IdentityUserRole.

Your class MyApplicationUser you have to implement must implement the right type:

public class MyApplicationUser : IdentityUser<string, MyUserLogin, ApplicationUserRoles, MyUserClaim>
{

}

and your role:

public class MyRole : IdentityRole<string, ApplicationUserRoles>
{
}

and your ApplicationDbContext:

public class MyContext : IdentityDbContext<MyUser, MyRole, string, MyUserLogin, ApplicationUserRoles, MyUserClaim>
{

}

and your UserStore:

public class MyUserStore:  UserStore<MyUser, MyRole, string, MyUserLogin, ApplicationUserRoles, MyUserClaim>
{
    public MyUserStore(MyContext context)
        : base(context)
    {
    }
}

That should be it, I guess. There's a github repo where I've played a bit with custom class and custom tables names.