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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With