Search code examples
model-view-controllercontrollerradix

ASP.Net MVC: How to redirect an uathenticated user from any page if they do not satisfy a condition


I need to redirect any Authenticated user to a specific page if they do not satisfy a condition.

I have implemented a base controller from which all controllers inherit. In its OnActionExecuting override I plan to perform my test for the condition and redirect to the specific action if the condition is not met.

Is this the most sensible way to perform this operation?


Solution

  • You could do it that way if you're planning on every action being screened or you could go the route of using custom Action Filters a la the HandleError filter.

    With filters you have access to the entire request stream and you can perform different actions. You can stack filters as well and even order them so they're executed in a specific order. Action filters can be applied at the class level too which means it will be applied to every action for you.