我使用教程here将用户数据库的主键从string更改为int,但是我在初始化Role Manager时遇到了困难。它曾经使用
进行初始化var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
如何使用新数据创建角色管理器?
更新:我使用MVC 5和EF 6。
答案 0 :(得分:29)
您的ApplicationRoleManager
可能如下所示。因为你必须从customRole类而不是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>()));
}
}
然后将以下代码添加到Startup.Auth.cs类(如果当前不存在。
)app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
然后,您可以创建和管理角色。
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);
}
希望这有帮助。
答案 1 :(得分:4)
根据您的问题,我无法确定您是否使用相同的框架(MVC和EF)。
最近,我使用自定义ApplicationUsers&amp; amp;创建了一个MVC + EF解决方案。 IdentityRoles。 ApplicationUser派生自“Microsoft.AspNet.Identity.EntityFramework.IdentityUser”。 ApplicationRoles无需更改即可实现。
我有以下课程:
// 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>()));
}
}
在configuration.cs(迁移)中==&gt;在'update-database'上将执行
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);
使用startup.auth.cs与RoleManager的链接:
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
答案 2 :(得分:1)
希望这有帮助。
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")
;
}
}