2

Is there any way to execute a method in a controller before any action is called automatically? For example, let's say I have the login credentials of the user stored in cookies, I have a user controller and any time any of the actions in that controller is called I wanna check if the user is logged in or not. (instead of calling that method in each action individually)

 public ActionResult Test () {
     if (Checklogin()) {
         return View();
     }
     else {
        return new EmptyView();
    }
 }

I wanna make all the methods follow this logic but without defining this in each one of them individually.

JAX
  • 1,540
  • 3
  • 15
  • 32
  • 2
    I'm not going to write up a full answer, but basically action filters, or http modules are probably what you're looking for. [This question](http://stackoverflow.com/questions/11507496/http-module-vs-action-filter-in-asp-net-mvc) talks about the differences. – Jon Egerton Aug 06 '14 at 20:40
  • 1
    Just beaten to it - yes - custom ActionFilter. http://msdn.microsoft.com/en-us/library/dd410056(v=vs.100).aspx. For this purpose, specifically a custom AuthorizeAttribute, which is derived from FilterAttribute. – M Smearer Aug 06 '14 at 20:40
  • The constructor can be used for injecting in any dependencies that the controller may require. Mvc has great out of the box support for dependency injection. I was going to say ActionFilters too. The beauty with them is you can apply them as attributes either at action level, or across a whole controller. – daniellepelley Aug 06 '14 at 20:41
  • @MSmearer: The methods in the ActionFilterAttribute class are void, so how can I define to them, for example `if (i > 0)` let the action execute else return an error? – JAX Aug 06 '14 at 21:11
  • 1
    @EhsanSajjad: Thanks but, that approach would double up the work! – JAX Aug 06 '14 at 21:27
  • how, you just have to create a basecontroller and all your controllers should inherit from that, if you use CustomactionFilterAttribute in that case as well you have to decorate your actions with it – Ehsan Sajjad Aug 06 '14 at 21:29
  • @EhsanSajjad: I think the link you provided is exactly what he wants! – Transcendent Aug 06 '14 at 21:40
  • Implement a custom AuthorizeAttribute by overriding OnAuthorization. Decorate a base controller class with you custom attribute. Inherit the base controller then _all_ actions for that controller will run your custom authorization. – Jasen Aug 06 '14 at 21:45
  • yes @Transcendent it is a proper way, i recently did same way in my application – Ehsan Sajjad Aug 06 '14 at 21:47

1 Answers1

3

As suggested in the comments use the ActionFilter. If your conditions are not met, like if the cookie is expired or something, call the action made for displaying the error message in the overridden method.

public class AuthenticationAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        if (Condition Not Met)
        {
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
            {
                controller = "Error Message Controller",
                action = "Error Message Action"
            }));
        }
    }
}
Transcendent
  • 5,598
  • 4
  • 24
  • 47