Initializing RoleManager in ASP.NET Identity with Custom Roles

后端 未结 3 2049
渐次进展
渐次进展 2020-12-30 03:19

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 initiali

相关标签:
3条回答
  • 2020-12-30 03:29

    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.

    0 讨论(0)
  • 2020-12-30 03:33

    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);
    
    0 讨论(0)
  • 2020-12-30 03:49

    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")
                ;
            }
        }
    
    0 讨论(0)
提交回复
热议问题