Search code examples
asp.net-mvc-3cookiescustom-action-filter

How to develop action filter controller or action method specific


i was trying to develop a custom action filter which will check cookie is enable or not. if cookie is not enable then redirect use to a specific error page.here is my code.

public class CheckCookieAttribute : FilterAttribute, IActionFilter
    {

       public string prmAction{get;set;}

       public string prmController{get;set;}


        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
        if(filterContext.HttpContext.Request.Cookie["YourCookie"]==null)
        {
            filterContext.Result = controller.RedirectToAction(prmAction,prmController)
        }
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //The action filter logic - after
        }
    }

now i am using like

[CheckCookie(prmAction="MyAction",prmController="MyController")]

due to lack of good knowledge i am not being able to develop attribute driven check for cookie enable or disable.

i want to develop a code in such a way as a result i should not pass any controller name or action name. i like to use code like

[HttpPost]
 [CheckCookieAttribute]
 public ActionResult Save(Person oPerson)
 {
        return View();
 }

[CheckCookieAttribute]
public class HomeController : Controller
{
public ActionResult Index()
{return View();}

public ActionResult About()
{return View();}
}
}

where i will not provide any name of controller or action name. just guide me what i need to change in my code. thanks


Solution

  • It seems that what you are trying to accomplish is already built into ASP.NET MVC.

    I would use the [Authorize] attribute (http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v=vs.108).aspx) where you want to check if the user has a cookie.

    If you want to redirect the user to a specific controller/action when the user is not authorized, you can use the following attribute instead:

    public class AuthorizeUserAttribute : AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary(
                        new
                        {
                            controller = "Error",
                            action = "Unauthorized"
                        })
                    );
        }
    }
    

    See ASP.NET MVC 4 Custom Authorize Attribute with Permission Codes (without roles)

    Then you would use it by using:

    [HttpPost]
    [AuthorizeUser]
    public ActionResult Save(Person oPerson)
    {
        return View();
    }
    

    Or if you want exactly what you asked for you can do it this way:

    public class CheckCookieAttribute : ActionFilterAttribute, IActionFilter
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if (filterContext.HttpContext.Request.Cookies["YourCookie"] == null)
                {                                
                    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { {"controller", "MyController"}, {"action", "MyAction"}});                
                }
                else
                {
                    base.OnActionExecuting(filterContext);
                }
            }
    
            public void OnActionExecuted(ActionExecutedContext filterContext)
            {
                //The action filter logic - after
            }
        }