正在使用自定义角色初始化ASP.NET标识中的RoleManager
本文关键字:标识 NET RoleManager ASP 初始化 自定义 角色 | 更新日期: 2023-09-27 18:25:28
我使用这里的教程将用户数据库的主键从string更改为int,但在初始化角色管理器时遇到了困难。它曾经使用进行初始化
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
如何使用新数据创建角色管理器?
更新:我使用的是MVC 5和EF 6。
您的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);
}
希望这能有所帮助。
根据您的问题,我无法确定您是否使用相同的框架(MVC和EF)。
最近,我使用自定义ApplicationUsers&身份角色。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(迁移)中===>在"更新数据库"上将执行
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);
希望这能有所帮助。
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")
;
}
}