gre.p
gre.p

Reputation: 319

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.

Upvotes: 18

Views: 39552

Answers (3)

a_walid
a_walid

Reputation: 11

Hope this helps.

    var roleManager = new RoleManager<CustomRole,int>(new RoleStore<CustomRole,int,CustomUserRole>(context));
    var UserManager = new UserManager<ApplicationUser,int>(new UserStore<ApplicationUser,CustomRole,int,CustomUserLogin,CustomUserRole,CustomUserClaim>(context));

   // In Startup iam creating first Admin Role and creating a default Admin User     
    if (!roleManager.RoleExists("Admin")){
    //first we create Admin rool   
        var role = new CustomRole();
        role.Name = "Admin";
        roleManager.Create(role);
    }

    ////Here we create a Admin super user who will maintain the website     
    if (UserManager.FindByName("Admin") == null){
        var user = new ApplicationUser() { UserName = "Admin" };
        string userPWD = "adminadmin";
        var chkUser = UserManager.Create(user, userPWD);
        if (chkUser.Succeeded){
            var result1 = UserManager.AddToRole(user.Id, "Admin")
            ;
        }
    }

Upvotes: 1

DSR
DSR

Reputation: 4668

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.

Upvotes: 32

Ronald van Meer
Ronald van Meer

Reputation: 41

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);

Upvotes: 4

Related Questions