locked
İs it possible to use methods between controllers in asp.net web api RRS feed

  • Question

  • User-651651378 posted

    I'm building a Web API in asp.net. i created a controller and all of the methods i will use is in this controller. I need to set authorizations in different controllers but i don't want to write all those codes again. Is there anyway to call that methods from different controllers?

    Tuesday, April 13, 2021 1:39 PM

All replies

  • User475983607 posted

    I'm building a Web API in asp.net. i created a controller and all of the methods i will use is in this controller. I need to set authorizations in different controllers but i don't want to write all those codes again. Is there anyway to call that methods from different controllers?

    The problem description is difficult to understand.  In general a request must be authorized to access a secured resource.  Actions or controllers that are protected by an [Authorize] attribute are secured. 

    If your code allows anonymous requests to execute secured code then your code has a logical bug.  Can you share a code sample that illustrates your security design and the problem you are trying to fix?

    Tuesday, April 13, 2021 1:57 PM
  • User287926715 posted

    Hi m.ulas,

    You can create a CommonController to restrict it, and then let all controllers that require authentication inherit from this controller.

    Or you can add a global filter. But for actions or controllers that do not require authentication, you need to add AllowAnonymous in front.

    You can view related documents.

    Authentication and Authorization in ASP.NET Web API


    Best Regards,

     ChaoDeng

    Thursday, April 15, 2021 5:53 AM