So my system requires that Roles have associated Expiry dates. I have implemented the Identity 2.0 framework, and things are going smoothly, but I've run into an issue that is making me doubt my structure.
public class ApplicationUserRole : IdentityUserRole
{
public override string UserId { get; set; }
public override string RoleId { get; set; }
public DateTime Expiry { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
static ApplicationDbContext()
{
Database.SetInitializer<ApplicationDbContext>(null);
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<ApplicationUserRole> ApplicationUserRoles { get; set; }
}
This does what I want it to. It changes the AspNetUserRoles table so that it adds a third column which is a DateTime called Expiry. I'm able to add, update, and remove expiries using the ApplicationDBContext, all works fine and well while editing Users.
The issue arises on Account Creation. The UserManager calls AddToRolesAsync, but only takes two parameters, User and Role. I need it to take User, Role, and an Expiry.
Do I need to implement my own UserManager? That seems like overkill.
I'm able to get around the issue by not allowing role/expiry selection on account creation, and just leaving it to edit mode. But I'd really like to be able to create accounts and assign roles/expiries at the same time.
I know this has been answered already - but in case anybody is interested - but instead of using UserManager.AddToRolesAsync, I looped through the new roles and used the Role.Add() method of User; like so:
Take a look at below video tutorial on using asp.net identity using existing database tables. I think you have to let ApplicationUser table know that you have new field in your ApplicationUserRole table. So that you have to follow entity framework model binding in order to achieve that.
Part 1 - https://www.youtube.com/watch?v=elfqejow5hM
Part 2 - https://www.youtube.com/watch?v=JbSqi3Amatw
You don't need to implement your own userManager, but you have to amend it. Hope you can use this example to support your needs.