Identityserver4添加角色和授权角色的方法

添加角色:

        public string AddRole(string roleName)
        {
            if (_roleManager.FindByNameAsync(roleName).Result == null)
            {
                var role = new IdentityRole
                {
                    Name = roleName
                };
                var result = _roleManager.CreateAsync(role).Result;
            }
            return "success";
        }

授权角色给用户:

        public string AddRolesWithUserName(string userName, string roleNames)
        {
            var user = _userManager.FindByNameAsync(userName).Result;
            var allRoles = _roleManager.Roles.ToList(); //系统中所有的角色
            var nowRoles = _userManager.GetRolesAsync(user).Result; //该用户现有的角色“Name”
            foreach (var nowRole in nowRoles)
            {
                var normalizedName = allRoles.Where(r => r.Name == nowRole).First().NormalizedName;//取得现有角色的NormalizedName
                var deleteResult = _userManager.RemoveFromRoleAsync(user, normalizedName).Result; //删除不要的角色
            }
            var result = _userManager.AddToRolesAsync(user, roleNames.Split(',')).Result; //添加需要的角色
            _userManager.UpdateAsync(user); //完成储存
            return "success";
        }

完整的controller

public class RoleController : ControllerBase
    {
        private readonly UserManager<IdentityUser> _userManager;
        private readonly RoleManager<IdentityRole> _roleManager;
        private readonly IIdentityServerInteractionService _interaction;
        private readonly IClientStore _clientStore;
        private readonly IAuthenticationSchemeProvider _schemeProvider;
        private readonly IEventService _events;

        public RoleController(
            UserManager<IdentityUser> userManager,
            RoleManager<IdentityRole> roleManager,
            IIdentityServerInteractionService interaction,
            IClientStore clientStore,
            IAuthenticationSchemeProvider schemeProvider,
            IEventService events)
        {
            _userManager = userManager;
            _roleManager = roleManager;
            _interaction = interaction;
            _clientStore = clientStore;
            _schemeProvider = schemeProvider;
            _events = events;
        }

        public string AddRole(string roleName)
        {
            if (_roleManager.FindByNameAsync(roleName).Result == null)
            {
                var role = new IdentityRole
                {
                    Name = roleName
                };
                var result = _roleManager.CreateAsync(role).Result;
            }
            return "success";
        }

        public string AddRolesWithUserName(string userName, string roleNames)
        {
            var user = _userManager.FindByNameAsync(userName).Result;
            var allRoles = _roleManager.Roles.ToList(); //系统中所有的角色
            var nowRoles = _userManager.GetRolesAsync(user).Result; //该用户现有的角色“Name”
            foreach (var nowRole in nowRoles)
            {
                var normalizedName = allRoles.Where(r => r.Name == nowRole).First().NormalizedName;//取得现有角色的NormalizedName
                var deleteResult = _userManager.RemoveFromRoleAsync(user, normalizedName).Result; //删除不要的角色
            }
            var result = _userManager.AddToRolesAsync(user, roleNames.Split(',')).Result; //添加需要的角色
            _userManager.UpdateAsync(user); //完成储存
            return "success";
        }

 

posted @ 2021-05-04 19:16  星星c#  阅读(384)  评论(0编辑  收藏  举报