Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems that what you are trying to accomplish is already built into ASP.NET MVC.</p> <p>I would use the [Authorize] attribute (<a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v=vs.108).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v=vs.108).aspx</a>) where you want to check if the user has a cookie. </p> <p>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:</p> <pre><code>public class AuthorizeUserAttribute : AuthorizeAttribute { protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary( new { controller = "Error", action = "Unauthorized" }) ); } } </code></pre> <p>See <a href="https://stackoverflow.com/questions/13264496/asp-net-mvc-4-custom-authorize-attribute-with-permission-codes-without-roles">ASP.NET MVC 4 Custom Authorize Attribute with Permission Codes (without roles)</a></p> <p>Then you would use it by using:</p> <pre><code>[HttpPost] [AuthorizeUser] public ActionResult Save(Person oPerson) { return View(); } </code></pre> <p>Or if you want exactly what you asked for you can do it this way:</p> <pre><code>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 } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload