/// <summary>
        /// 在登陆的时候把角色权限的令牌添加进去
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task AddClaimAndSignIn(User user)
        {
            var roles = await _roleSever.GetRoleByUserId(user.Id);

            List <int> roleIds = new List <int>();

            foreach (var item in roles)
            {
                roleIds.Add(item.Id);
            }
            var roleClaims = _roleClaimsSever.GetRoleClaims(roleIds);

            //var claims = new[]
            //      {
            //          new Claim(ClaimTypes.NameIdentifier,user.Id.ToString()),
            //          new Claim(ClaimTypes.Name,user.UserName)
            //      };
            var claimIdentity = new ClaimsIdentity(IdentityConstants.ApplicationScheme);

            claimIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
            claimIdentity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
            foreach (var item in roleClaims)
            {
                claimIdentity.AddClaim(new Claim("RoleClaims", item.ClaimValue));
            }
            var claimPrincipal = new ClaimsPrincipal(claimIdentity);
            await _httpContextAccessor.HttpContext.SignInAsync(IdentityConstants.ApplicationScheme, claimPrincipal, new AuthenticationProperties
            {
                IsPersistent = true
            });
        }
Esempio n. 2
0
        public async Task <IActionResult> EditRole(int Id)
        {
            var role = await _roleSever.GetRoleByRoleId(Id);

            AddAndEditRoleViewModels addAndEditRoleViewModels = new AddAndEditRoleViewModels();
            var model          = Mapper.Map <AddAndEditRoleViewModels>(role);
            var roleFictitious = RoleFictitious;

            model.RolePermissions = RoleFictitious.ToList();
            //var user=_httpContextAccessor.HttpContext.User;
            var claims = _roleClaimsSever.GetRoleClaims(new List <int>()
            {
                Id
            });

            RecursivelyPermission(model.RolePermissions, claims);
            return(View(model));
        }