In the System.Web.Httpaplication.AuthorizeRequest
event, which object exposes the controller and action that the request is responding to?

- 14,854
- 31
- 111
- 198
2 Answers
Short answer: it's not possible with built-in functionality, because AFAIK at that point there is no MVC at all yet. The event is before any MVC processing would start.
However, you can do the manual hacking to mimic the MVC lifecycle and get a controller context at the end, but it's not too elegant. If you want to try that path, check this SO answer out: https://stackoverflow.com/a/2159134/1323504
I would recommend to redesign your task, because there are built-in places where request authorization could be, for example global filter attributes, etc.

- 1
- 1

- 12,249
- 8
- 65
- 93
-
Thanks. Where do I trap [Authorize] when it executes? I see a onAuthorization method. – Old Geezer Feb 28 '15 at 09:24
-
1I don't think, but you can create your own custom fitler attribute, and add it to global filters. This way it will execute before any executed action, so you can do your checkings and authorization logic there. You have the ControllerContext instance at that point. Please google for MVC global filters, there are plenty of articles and posts related. – Zoltán Tamási Feb 28 '15 at 09:27
-
For example check out this post, you need an Action Filter I think. The article is for MVC 4, but there is no significant change in this topic from MVC3. http://www.asp.net/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-custom-action-filters – Zoltán Tamási Feb 28 '15 at 09:29
If you want to change the behavior of the Authorize attribute, you can subclass it.
However, if you want to provide some other functionality based on the OnAuthorization event, you can implement IAuthorizationFilter.

- 55,572
- 24
- 139
- 212