5

I am new to MVC 5 asp.net Identity model and was looking for means to customise standard Asp.net Identity to suit my needs. Through blog on TypeCast Exception and one at Stackoverflow: How to model Identity I was able to create my own elements in ApplicationUser and ApplicationRole tables. However, my requirement is to add new columns to UserRole table, DATE_FROM and DATE_TO which I did by implementing IdentityUserRole interface. My problem is when I'm trying to save the link UserManager.AddRoleToUser takes only two parameters, UserName and RoleName. How to store parameters for custom ApplicationUserRole ?

public bool AddUserToRole(string userId, SelectUserRolesViewModel roleName)
{
                var um = new UserManager<ApplicationUser>(
                    new UserStore<ApplicationUser>(new ApplicationDbContext()));


                var idResult = um.AddToRole(userId, roleName);
                return idResult.Succeeded;
            }

the SelectUserRolesViewModel supplies extended IdnetityUserRole model. Any pointer will be appreciated.

Community
  • 1
  • 1
siddhant Kumar
  • 411
  • 8
  • 20
  • To clarify, I have successfully added new attributes to IdentityUser and IdentityRole. Now my requirement is to assign roles to users which are valid only between DATE_FROM and DATE_TO. For this, i have extended `IdentityUserRole` interface to include these fields. However I'm not able to save them as UserManager.AddToRole does not accept custom class. – siddhant Kumar Feb 11 '14 at 09:56

1 Answers1

3

If you add additional properties to the ApplicationUserRole table, you can't use AddUserToRole method anymore. Because AddUserToRole method is from UserManagerExtensions class which is sealed class, you cann't create your own class to inherit from UserManagerExtensions. I'm not sure there are any better solution for this, but below is a working example.

Add additional properties to the ApplicationUserRole table:

public class ApplicationUserRole : IdentityUserRole
{
    public DateTime DateFrom { get; set; }
    public DateTime DateTo { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
    public DbSet<ApplicationUserRole> ApplicationUserRoles { get; set; }
}

Then you can create a new ApplicationUserRole instance like below:

    using (var _db = new ApplicationDbContext())
    {
        var roleName = //Get role name from somewhere here
        var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(_db));
        if (!roleManager.RoleExists(roleName))
        {
             var newRoleresult = roleManager.Create(new IdentityRole()
             {
                  Name = roleName,
             });
        }
        var userRole = new ApplicationUserRole
        {
            UserId = currentUser.Id,
            RoleId = roleManager.FindByName(roleName).Id,
            DateFrom = DateTime.Now,
            DateTo = DateTime.Now.AddDays(1)
        };
        _db.ApplicationUserRoles.Add(userRole);
        _db.SaveChanges();
   }
Lin
  • 15,078
  • 4
  • 47
  • 49
  • If I'm not mistaken you are creating attributes for IdentityRole table. What I wanted is to assign a Role to User which is valid between DATE_FROM and DATE_TO. for this purpose I added two attributes to ApplicationUserRole (extending IdentityUserRole interface) – siddhant Kumar Feb 11 '14 at 09:16
  • hi @siddhant4u, sorry I misunderstood your question. I updated my answer, take a look at it. – Lin Feb 11 '14 at 15:36
  • Thanks. That fixed my issue. Now I can design my custom security around your work :) – siddhant Kumar Feb 11 '14 at 16:09