title: How can I have ASP.NET automatically redirect non-logged in Forms users to the login page?
body: <p>I have an <a href="http://en.wikipedia.org/wiki/ASP.NET" rel="noreferrer">ASP.NET</a> website.</p>
  
  <p>I want users who are not logged in to be automatically (re)directed to the login page, for example,</p>
  
  <pre><code>~/Account/Login.aspx
  </code></pre>
  
  <p>As it is now, users are able to visit pages (for example, <code>default.aspx</code>) without being logged in.</p>
  
  <hr>
  
  <p><strong>Note</strong>: I am operating on the (perhaps incorrect) assumption that ASP.NET has its own authentication cycle that happens behind my back before every (and any) page loads.</p>
  
  <hr>
  
  <p><strong>Update</strong> @asawyer provided a link that, while not helping to answer the question, did provide a pretty graphic:</p>
  
  <p><img src="https://i.stack.imgur.com/X2OIN.png" alt="Enter image description here"></p>
  
  <h2>Well, what have you tried?</h2>
  
  <p>I have a <code>web.config</code> file that enables <strong>Forms</strong> authentication:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  ...
  &lt;configuration&gt;
     ...
     &lt;system.web&gt;
        &lt;authentication mode="Forms"&gt;
           &lt;forms loginUrl="~/Account/Login.aspx" name=".ASPXFORMSAUTH" slidingExpiration="true"/&gt;
        &lt;/authentication&gt;
        ...
     &lt;/system.web&gt;
     ...
  &lt;/configuration&gt;
  </code></pre>
  
  <p>When i browse to the "default" page, I am able to view it, for example,</p>
  
  <pre><code>GET http://localhost:53149/WebSite/ HTTP/1.1
  Host: localhost:53149
  </code></pre>
  
  <p>And I'm get the page contents:</p>
  
  <pre><code>HTTP/1.1 200 OK
  Server: ASP.NET Development Server/10.0.0.0
  </code></pre>
  
  <p>In other words, rather than being <em>forced</em> to login to the web-site, I am <strong><em>not</em></strong> being forced to log in to the web-site.</p>
  
  <p>It might be related to the fact that my browser is running locally to the web-server; but I'm using <code>Forms</code>, not <code>Windows</code> (and not <code>Passport</code> and not <code>None</code>) authentication.</p>
  
  <h2>Bonus Reading</h2>
  
  <ul>
  <li><a href="http://www.asp.net/web-forms/tutorials/security/introduction/an-overview-of-forms-authentication-cs" rel="noreferrer">An Overview of Forms Authentication (C#)</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/9wff0kyh.aspx" rel="noreferrer">MSDN: Forms Authentication Provider</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/330a99hc.aspx" rel="noreferrer">MSDN: ASP.NET Web Application Security</a></li>
  <li><a href="https://wiki.jasig.org/display/CASC/ASP.NET+Forms+Authentication" rel="noreferrer">ASP.NET Forms Authentication</a></li>
  <li><a href="https://stackoverflow.com/questions/1574735">Require the user to log in to view a document using ASP.NET</a></li>
  <li><a href="https://stackoverflow.com/questions/2900931">Protect some pages from direct access in ASP.NET</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/k3fc21xw" rel="noreferrer">FormsAuthentication Class</a></li>
  </ul>