3

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.

enter image description here

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.

Learning Curve
  • 1,449
  • 7
  • 30
  • 60
  • Are you just trying to change the table name of AspNetUserRoles? http://stackoverflow.com/questions/22855428/how-to-change-table-names-for-asp-net-identity-2-0-with-int-id-columns – Andy Wiesendanger Sep 24 '15 at 13:48
  • NO. AspNetUserRoles table is still there with deafult columns (UserId and RoleId). Initially i tried to add ApplicationId as a composite key column in AspNetUserRoles but ended up having more problems than with current implementation migration has created a new table which is ApplicationUserRole and I want UserManager to bring role information from this ApplicationUserRole table rather the AspNetUserRole table. – Learning Curve Sep 24 '15 at 14:03
  • So you want AspNetUserRoles table, you just won't use it? – Andy Wiesendanger Sep 24 '15 at 14:10
  • I have no interest in AspNetUserRole tables since I have ApplicationUserRoles table which is exactly i need. In bit more detail, AspNetUserRole table allow only one role per UserId but in my case same Role can be assigned to same User but in different applications and that's the reason I have created this new ApplicationUserRoles table. – Learning Curve Sep 24 '15 at 14:13
  • Right, so if you see that link, you can change table name, so usermanager will use your table. – Andy Wiesendanger Sep 24 '15 at 14:15
  • A bit more detail to the above comment: userIdentity created by the manager.CreateIdentityAsync in IdentityModel not showing me the roles from my custom table as it is still grabbing role base information from AspNetUserRole table. So at this point I am wondering how I can replace the AspNetUserRole with ApplicationUserRole while UserManager create the userIdentity. – Learning Curve Sep 24 '15 at 14:17
  • I am getting the following exception when I add modelBuilder.Entity().ToTable("ApplicationUserRoles"); The entity types 'IdentityUserRole' and 'ApplicationUserRoles' cannot share table 'ApplicationUserRoles' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them. – Learning Curve Sep 24 '15 at 14:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90535/discussion-between-learning-curve-and-andy-wiesendanger). – Learning Curve Sep 24 '15 at 14:25
  • @LearningCurve: Did you manage to find a solution for this? Cheers. – LeftyX Sep 30 '15 at 08:24
  • @LeftyX: Thanks for your reply. I got involved with other important work but will definitely let you know as soon as I get back to this. – Learning Curve Oct 01 '15 at 11:30

1 Answers1

10

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.

LeftyX
  • 35,328
  • 21
  • 132
  • 193