Initializing RoleManager in ASP.NET Identity with Custom Roles

后端 未结 3 2063
渐次进展
渐次进展 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:49

    Hope this helps.

        var roleManager = new RoleManager(new RoleStore(context));
        var UserManager = new UserManager(new UserStore(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")
                ;
            }
        }
    

提交回复
热议问题