Last post Dec 13, 2017 09:14 PM by Dmitry Sikorsky
Dec 13, 2017 06:39 PM|webbert|LINK
If I have [Authorize] turned on for all controllers and methods via configuration and have a custom middleware that I have up and running as well.
What I would like, is to make a particular middleware run conditionally based on whether the method that is to be run is marked [AllowAnonymous]. If the method is marked [AllowAnonymous], I do NOT want to run the middleware.
It looks like UseWhen might be the answer but I can't seem to figure out how to make it work.
Any help is appreciated.
Dec 13, 2017 08:38 PM|Dmitry Sikorsky|LINK
Hi! It is simple. Here is the sample:
(It adds a middleware based on the URL, but you can use any other condition.)
Dec 13, 2017 08:49 PM|webbert|LINK
Right, but the condition is what I am trying to figure out.
How do I identify the endpoint and determine if it is marked as allow anonymous
Dec 13, 2017 09:14 PM|Dmitry Sikorsky|LINK
So, our task is to get the controller and action from the context. I think the context may know nothing about the controllers or actions, because it is controlled by the middleware (which is neutral to MVC). But maybe we can try to get the route parameters,
then using reflection get the type by controller name, and its action using the action name. And then get its attributes.