title: Using Global.asax to set/check session variable and redirect (for user testing)
body: <p>I would like to add very simple, temporary security to my site.  </p>
  
  <p>I made a page at Home/UnderConstruction where people testing the site can enter a hard-coded password which will then set the "underconstruction" session variable to "false". </p>
  
  <p>This is what I have so far, but it results in too many redirects:</p>
  
  <pre><code>    protected void Session_Start(Object sender, EventArgs e)
      {
          HttpContext.Current.Session["underconstruction"] = "true";
      }
  
      protected void Application_AcquireRequestState(Object sender, EventArgs e)
      {
                  if (HttpContext.Current != null &amp;&amp; HttpContext.Current.Session != null)
                  {
                      var underconstruction = HttpContext.Current.Session["underconstruction"];
                      if (underconstruction != null)
                      {
                          string oc = underconstruction.ToString();
                          if (oc != "false") Response.Redirect("~/Home/UnderConstruction");
                      }
                  }
  
      }
  </code></pre>
  
  <p>Is this close to what I would need to do?</p>
  
  <p><strong>Here is the code we got to work:</strong></p>
  
  <p><strong>Controller Code for UnderConstruction View</strong></p>
  
  <pre><code>    public ViewResult UnderConstruction()
      {
          return View();
      }
  
  
      [HttpPost]
      public ActionResult UnderConstruction(string ocp)
      {
          if (ocp == "mypassword")
          {
              Session["underconstruction"] = "false";
              return RedirectToAction("Index", "Home");
          }
          else
          {
              Session["beingredirected"] = "false";
              return View();
          }
      }
  </code></pre>
  
  <p><strong>Global.Asax</strong></p>
  
  <pre><code>    protected void Session_Start(Object sender, EventArgs e)
      {
          HttpContext.Current.Session["underconstruction"] = "true";
          HttpContext.Current.Session["beingredirected"] = "false";
      }
  
  
      protected void Application_AcquireRequestState(Object sender, EventArgs e)
      {
          if (HttpContext.Current != null &amp;&amp; HttpContext.Current.Session != null)
          {
              bool uc = false;
              var underconstruction = HttpContext.Current.Session["underconstruction"];
              if (underconstruction != null)
              {
                  uc = Boolean.Parse(underconstruction.ToString());
              }
  
              bool redirected = false;
              var beingredirected = HttpContext.Current.Session["beingredirected"];
              if (beingredirected != null)
              {
                  redirected = Boolean.Parse(beingredirected.ToString());
              }
  
              if (uc &amp;&amp; !redirected)
              {
                  if (Request.HttpMethod == "GET")
                  {
                      HttpContext.Current.Session["beingredirected"] = "true";
                      Response.Redirect("~/Home/UnderConstruction");
                  }
                  else if (Request.HttpMethod == "POST")
                  {
                  }
  
              }
  
              HttpContext.Current.Session["beingredirected"] = "false";
          }
      }
  </code></pre>