示例#1
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = null;
            var authorized = false;

            if (String.IsNullOrWhiteSpace(Roles) &&
                String.IsNullOrWhiteSpace(Permissions))
            {
                return;
            }

            if (!String.IsNullOrWhiteSpace(Roles))
            {
                var roles = new StringTokenizer(Roles, Separator);

                foreach (var item in roles)
                {
                    var role = item.Trim();

                    if (role.HasValue && role.Length > 0)
                    {
                        authorized = httpContextAccessor.HttpContext.User.IsInRole(role.Value);

                        if (authorized)
                        {
                            break;
                        }
                    }
                }
            }

            if (!String.IsNullOrWhiteSpace(Permissions))
            {
                var permissions = new StringTokenizer(Permissions, Separator);

                foreach (var item in permissions)
                {
                    var permission = item.Trim();

                    if (permission.HasValue && permission.Length > 0)
                    {
                        authorized = await permissionEvaluator.HasPermissionAsync(
                            httpContextAccessor.HttpContext.User,
                            permission.Value);

                        if (authorized)
                        {
                            break;
                        }
                    }
                }
            }

            if (!authorized)
            {
                output.SuppressOutput();
            }
        }
        protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement)
        {
            if (context.User.Identity.IsAuthenticated)
            {
                if (await _permissionEvaluator.HasPermissionAsync(context.User, requirement.Name))
                {
                    context.Succeed(requirement);
                    return;
                }
            }

            context.Fail();
        }