MVC 3 How can I make a user view a warning/disclaimer screen

asp.net-mvc, asp.net-mvc-3, c#

Solution

You could make a custom attribute and add it to the top of the Controller.

For example:

    [AgreedToDisclaimer]
    public ActionResult LoadPage()
    {
         return View();
    }

Which would only load the view if the AgreedToDisclaimer returns true.

public class AgreedToDisclaimerAttribute : AuthorizeAttribute
{

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
       if (httpContext == null)
        throw new ArgumentNullException("httpContext");

       // logic to check if they have agreed to disclaimer (cookie, session, database)
       return true;
    }

   protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
   {
          // Returns HTTP 401 by default - see HttpUnauthorizedResult.cs.
           filterContext.Result = new RedirectToRouteResult(
            new RouteValueDictionary 
            {
             { "action", "ActionName" },
             { "controller", "ControllerName" },
             { "parameterName", "parameterValue" }
            });
   }
}

http://msdn.microsoft.com/en-us/library/dd410209(v=vs.90).aspx

http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.handleunauthorizedrequest.aspx

Problem

I thought this would be very simple but I'm struggling a little. I'm working on a project for a client using MVC 3 that requires users to agree to certain conditions before using the site. I have created a standard agree/disagree screen which is loaded when first coming into the site, but if a user types a address to a different part of the site they can bypass the conditions for example www.test.com loads the conditions but if the user types www.test.com/home they bypass the conditions. How can I make sure they have agreed to the conditions before they can get anywhere else on the site? I have been trying a session variable, which I think is the way to go, but is there a way to check this variable on every page request without having to write a check into every Controller Action on the site?

Original source