0

I am trying to implement Authorization in my .Net Core web api and after setting up my program.cs and after "decorating" the desired method with the [Authorize("ArchiveList")] I am having problems with handling the request in my HandleRequirement method.

Here's the example of the object I get when I try to execute a method with the [Authorize] attribute on top of it.

I want to be able to access the name of the method that is making the request, and that is in the context.Resource -> Field but when I try to read context.Resource it only gives me the string "HotChocolate.Execution.Processing.MiddlewareContext` which is not what I really need/want. I can share the program.cs authorization coding part if needed. Thank you in advance.

leoxrmd
  • 17
  • 2
  • Sorry i am sure what do you mean about `I want to be able to access the name of the method that is making the request`, Do you want to get the name of method which the current request access to? – Xinran Shen Jun 16 '23 at 05:33
  • Hi @XinranShen, I mean I want to get the name of the method (with the [Attribute] notations on top) that is trying to making a request which triggers this `HandleRequirementAsync` method. – leoxrmd Jun 16 '23 at 12:48

1 Answers1

0

If you wanna get the name of the action with [Authorize] attribute in HandleRequirementAsync method, you can use below code to achieve it.

protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, SingleBuildingPolicyRequirement requirement)
    {
        var endpoint = context.Resource as HttpContext;

        if (endpoint != null) {
            var actioname = endpoint.Request.RouteValues["action"].ToString();
        }
        //........
}

enter image description here

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12