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.
If you look at the signature of
IdentityUser
(your ApplicationUser inherits from it)you can see that it accepts your custom definition of
IdentityUserRole
.Your class
MyApplicationUser
you have to implement must implement the right type:and your role:
and your
ApplicationDbContext
:and your
UserStore
:That should be it, I guess. There's a github repo where I've played a bit with custom class and custom tables names.