Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing RoleManager in ASP.NET Identity with Custom Roles

I changed the primary key for the user database from string to int using the tutorial here, but I'm having difficulty initializing the Role Manager. It used to be initialized using

var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

How can I create the role manager using the new data?

Update: I'm using MVC 5 and EF 6.

like image 741
gre.p Avatar asked Oct 03 '14 07:10

gre.p


2 Answers

Your ApplicationRoleManager may look like this. because you have to inherit from your customRole class instead of IdentityRole.

public class ApplicationRoleManager : RoleManager<CustomRole, int>
{
    public ApplicationRoleManager(IRoleStore<CustomRole, int> roleStore)
        : base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        return new ApplicationRoleManager(new RoleStore<CustomRole, int, CustomUserRole>(context.Get<ApplicationDbContext>()));
    }
}

Then add following code to Startup.Auth.cs class if not currently exists.

app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

Then you create and manage roles.

    var roleManager = HttpContext.GetOwinContext().Get<ApplicationRoleManager>();

    const string roleName = "Admin";

    //Create Role Admin if it does not exist
    var role = roleManager.FindByName(roleName);
    if (role == null)
    {
        role = new CustomRole();
        role.Id = 1; // this will be integer
        role.Name = roleName;

        var roleresult = roleManager.Create(role);
    }

Hope this helps.

like image 111
DSR Avatar answered Oct 07 '22 00:10

DSR


From your question i cannot determine if you are using the same frameworks (MVC & EF).

Recently i have created an MVC + EF solution using custom ApplicationUsers & IdentityRoles. ApplicationUser is deriving from "Microsoft.AspNet.Identity.EntityFramework.IdentityUser". ApplicationRoles are implemented without changes.

I have the following class:

// Configure the used in the application. RoleManager is defined in the ASP.NET Identity core assembly
    public class ApplicationRoleManager : RoleManager<IdentityRole>
    {
        public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
            : base(roleStore)
        {
        }

        public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
        {
            return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
        }
    }

within configuration.cs (migrations)==> on 'update-database' this will be executed

    var roleStore = new RoleStore<IdentityRole>(context);
    var roleManager = new RoleManager<IdentityRole>(roleStore);
    var applicationRoleAdministrator = new IdentityRole("Administrator");
    if (!roleManager.RoleExists(applicationRoleAdministrator.Name))
    {
       roleManager.Create(applicationRoleAdministrator);
    }
// do some logic to find your applicationUserAdministrator
var applicationUserAdministrator = userManager.FindByName("Administrator");
userManager.AddToRole(applicationUserAdministrator.Id, applicationRoleAdministrator.Name);

withing startup.auth.cs the linkage towards the RoleManager:

app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
like image 38
Ronald van Meer Avatar answered Oct 06 '22 22:10

Ronald van Meer