0

I'm having a bit of trouble with a WebAPI project using MVC5 and attribute routing.

Essentially, this is what I want to do:

[Route("Products/{CategoryId}")]
[ClaimsAuthorize([{CategoryId}], ClaimsEnum.CanViewProducts)]
public Products Get(){
  ...
}

Where [{CategoryId}] is where I want to pass the CategoryId to the attribute handler. The CustomAttribute handler looks like this:

public ClaimsAuthorizeAttribute(string CategoryId, params ClaimsEnum[] requiredClaimTypes)

Where ClaimsAuthorize is a custom attribute overriding the Authorize attribute.

How can I access the CategoryId parameter from the custom attribute? Is this possible? Or is there another way of passing it to the attribute handler?

This is pseudocode, but describes my problem. I need to know the category that is passed with the request URL when I'm processing the authorization. I know I can use headers, but I want to keep things visible in the URL if I can.

Thanks :)

Astravagrant
  • 685
  • 10
  • 18

1 Answers1

0

I ended up using this approach:

[ProtectedController]

[Route("Products/{CategoryId}")]
[ClaimsAuthorize(ClaimsEnum.CanViewProducts)]
public Products Get(){
  ...
}

ClaimsAuthorizeAttribute.cs

protected override bool IsAuthorized(HttpActionContext actionContext)
        {
_categoryId = actionContext.ControllerContext.RouteData.Values["CategoryId"].ToString();
Astravagrant
  • 685
  • 10
  • 18