You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ocelot网关中配置RouteClaimsRequirement以支持多Claim值及存在性校验?

Alright, let's tackle your two RouteClaimsRequirement issues step by step—turns out both scenarios are supported, you just need the right configuration approach.

Understanding RouteClaimsRequirement for Your Order Endpoints

First, let's align on your core requirements:

  • Order Create API: Requires the order_perm claim to be either "order_create" or "order_edit"
  • Order Query API: Only needs the order_perm claim to exist (any value is acceptable)

Scenario 1: App Crashes When Using an Array for order_perm

The crash happens because the default RouteClaimsRequirement doesn’t natively support array values for claim validation. When you pass ["order_create", "order_edit"], the configuration system can’t interpret that as a valid claim check rule.

Fix for Scenario 1

You have two reliable paths to solve this:

  1. Use a custom authorization policy (simplest approach)
    Define a policy in your startup code that explicitly checks if the order_perm claim matches either allowed value:

    services.AddAuthorization(options =>
    {
        options.AddPolicy("OrderCreateOrEdit", policy =>
            policy.RequireClaim("order_perm", "order_create", "order_edit"));
    });
    

    Then apply this policy to your order create endpoint:

    [Authorize(Policy = "OrderCreateOrEdit")]
    [HttpPost("orders")]
    public IActionResult CreateOrder(...)
    {
        // Your endpoint logic here
    }
    
  2. Config-driven allowed values (if you prefer externalized rules)
    If you want to manage allowed values via appsettings instead of hardcoding, bind the config array to a custom requirement handler:

    • In appsettings.json:
      "OrderPermissions": {
          "CreateAllowedValues": ["order_create", "order_edit"]
      }
      
    • Create a custom requirement and handler:
      public class OrderPermRequirement : IAuthorizationRequirement
      {
          public IEnumerable<string> AllowedValues { get; }
      
          public OrderPermRequirement(IEnumerable<string> allowedValues)
          {
              AllowedValues = allowedValues;
          }
      }
      
      public class OrderPermHandler : AuthorizationHandler<OrderPermRequirement>
      {
          protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, OrderPermRequirement requirement)
          {
              if (context.User.HasClaim(c => c.Type == "order_perm" && requirement.AllowedValues.Contains(c.Value)))
              {
                  context.Succeed(requirement);
              }
              return Task.CompletedTask;
          }
      }
      
    • Register the policy and handler in startup:
      var allowedValues = Configuration.GetSection("OrderPermissions:CreateAllowedValues").Get<string[]>();
      services.AddAuthorization(options =>
      {
          options.AddPolicy("OrderCreateOrEdit", policy =>
              policy.AddRequirements(new OrderPermRequirement(allowedValues)));
      });
      services.AddScoped<IAuthorizationHandler, OrderPermHandler>();
      

Scenario 2: User with order_perm: "create_order" Fails Authorization

Setting "order_perm": "" in RouteClaimsRequirement doesn’t work because it’s checking for an exact match of an empty string—not just the presence of the claim.

Fix for Scenario 2

To enforce that the order_perm claim exists (regardless of its value), use one of these options:

  1. Simple "claim exists" policy
    Define a policy that only verifies the claim is present:

    services.AddAuthorization(options =>
    {
        options.AddPolicy("OrderPermExists", policy =>
            policy.RequireClaim("order_perm"));
    });
    

    Apply this to your order query endpoint:

    [Authorize(Policy = "OrderPermExists")]
    [HttpGet("orders")]
    public IActionResult GetOrders(...)
    {
        // Your endpoint logic here
    }
    
  2. Config-based workaround (if you must use RouteClaimsRequirement)
    The default RouteClaimsRequirement doesn’t support "claim exists" checks directly via config. While you could build a custom route authorization handler to interpret empty config values as "claim exists", the policy approach is far cleaner and easier to maintain.

Key Takeaways

  • RouteClaimsRequirement is built for exact claim value matches—it doesn’t support arrays or "claim exists" checks out of the box.
  • For complex rules (array values, existence checks), custom authorization policies are the most flexible and maintainable solution.

内容的提问来源于stack exchange,提问作者kvuong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 17:12:51