title: How to Redirect User to Login Page if User Directly Accesses Any Page in ASP.NET WebForms
body: <p>I am working on ASP.NET webforms. I want to redirect the user to login page if user directly enters the URL of any other page. I have added the below code in web.config file and it redirects the user to login page in case the user tries to access any other page directly but there is an issue that when user logs in, it should redirect the user to welcome.aspx page but it redirects to login page again. where is the issue? </p>
  
  <p>code in web.config file i used....</p>
  
  <pre><code>&lt;authentication mode="Forms"&gt;
    &lt;forms name=".COOKIE" loginUrl="login.aspx" protection="All" path="/" timeout="480"/&gt;
  &lt;/authentication&gt;
  &lt;authorization&gt;
    &lt;deny users="?"/&gt;
    &lt;allow users="*"/&gt;
  &lt;/authorization&gt;
  </code></pre>
  
  <p>and
  login.aspx page code is as:</p>
  
  <pre><code>foreach (var user in db.usertables)
  
          if (user.username == TextBox1.Text &amp;&amp; user.password == TextBox2.Text)
          {
  
              {
                  Response.Redirect("welcome.aspx");
              }
          }
          else
  
          {
  
              Label1.Visible = true;
  
          }
  </code></pre>