Exemplo n.º 1
0
        public async Task <IActionResult> ManageUserRoles(string userId)
        {
            ViewBag.userId = userId;

            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"无法找到 Id {userId} 的用户";
                return(View("NotFound"));
            }

            var model = new List <RolesInUserViewModel>();

            foreach (var role in _roleManager.Roles)
            {
                var rolesInUserViewModel = new RolesInUserViewModel
                {
                    RoleId     = role.Id,
                    RoleName   = role.Name,
                    IsSelected = await _userManager.IsInRoleAsync(user, role.Name)
                };

                model.Add(rolesInUserViewModel);
            }

            return(View(model));
        }
        public async Task <IActionResult> EditRolesInUser(string userId)
        {
            ViewBag.userId = userId;

            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"User with Id = {userId} cannot be found";
                return(View("NotFound"));
            }

            var modelList = new List <RolesInUserViewModel>();

            foreach (var role in _roleManager.Roles)
            {
                var model = new RolesInUserViewModel
                {
                    RoleId   = role.Id,
                    RoleName = role.Name
                };

                model.IsSelected = await _userManager.IsInRoleAsync(user, role.Name) ? true : false;

                modelList.Add(model);
            }

            return(View(modelList));
        }
        //[Authorize(Policy ="EditRolePolicy")]
        public async Task <IActionResult> ManageUserRoles(string userId)
        {
            ViewBag.userId = userId;
            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                ViewBag.ErrorMessage($"无法找到ID为:{userId}的用户");
                return(View("NotFound"));
            }

            var model = new List <RolesInUserViewModel>();
            var roles = await _roleManager.Roles.ToListAsync();

            foreach (var item in roles)
            {
                var rolesInUserViewModel = new RolesInUserViewModel()
                {
                    RoleId   = item.Id,
                    RoleName = item.Name
                };
                //判断当前用户是否已经在该角色中
                if (await _userManager.IsInRoleAsync(user, item.Name))
                {
                    //将已拥有的角色信息设置为选中
                    rolesInUserViewModel.IsSelected = true;
                }
                else
                {
                    rolesInUserViewModel.IsSelected = false;
                }
                model.Add(rolesInUserViewModel);
            }
            return(View(model));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> ManageUserRoles(string userId)
        {
            ViewBag.userId = userId;

            var user = await userManager.FindByIdAsync(userId);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"无法找到ID为{userId}的用户";
                return(View("NotFound"));
            }

            var model = new List <RolesInUserViewModel>();

            //直接用roleManager.Roles会在userManager.IsInRoleAsync时出错
            var roles = await roleManager.Roles.ToListAsync();

            foreach (var role in roles)
            {
                var rolesInuserViewModel = new RolesInUserViewModel
                {
                    RoleId   = role.Id,
                    RoleName = role.Name,
                };

                //判断当前用户是否已经拥有该角色信息
                if (await userManager.IsInRoleAsync(user, role.Name))
                {
                    //将已拥有的角色信息设置为选中
                    rolesInuserViewModel.IsSelected = true;
                }
                else
                {
                    rolesInuserViewModel.IsSelected = false;
                }

                //添加已经角色到新的视图模型列表
                model.Add(rolesInuserViewModel);
            }

            return(View(model));
        }