I have implemented the Application_OnAuthenticateRequest method in my Global.asax class in my ASP.NET MVC 3 application. When a user hits a page on my application, I load the user and verify their account status. When their account status does not match the valid account statuses, I want to redirect that user to a controller/action to pe开发者_C百科rform additional actions.
The problem I am having is that I need to get the controller/action from the request to ensure that an infinite loop of redirects doesn't occur. I also am not sure of the best way to handle the redirect in this method to my new controller/action.
Any way to do this?
It is better to put your logic inside a custom Authroze Filter instead of Application_OnAuthenticateRequest method. Check this link
In your controller you could put [Authorize] before your method signature and this will redirect the user to another view if they are not authorized.
How about a Custom Method to authorize the user and Try/Catch? In "Catch" send him to the View you want.
精彩评论