title: ASP.NET Default page redirect when entering role restricted webpage
body: <p>I am using VS2005 ASP.NET 2.0.</p>
  
  <p>I am using web.config file to for my role management for specific role privileged webpages with the following code in my global (not in the Administrator folder) web.config file:</p>
  
  <pre><code>&lt;location path="Administrator"&gt;
      &lt;system.web&gt;
          &lt;authorization&gt;
              &lt;allow roles="Administrator"/&gt;
              &lt;deny roles="User"/&gt;
              &lt;deny users="?" /&gt;
          &lt;/authorization&gt;
      &lt;/system.web&gt;
  &lt;/location&gt;
  </code></pre>
  
  <p>The webpage can be role restricted, thus disallowing users without the role of <em>Administrator</em> to access.</p>
  
  <p>However, when users of any role tries to enter the restricted webpage, they will be automatically redirected to the login page with URL like </p>
  
  <p><code>http://localhost:2232/App/Login.aspx?ReturnUrl=%2fSoD%2fAdministrator%2fUserMgmt.aspx</code></p>
  
  <p>This will require the user to press the button back to go back to the previous page.</p>
  
  <p>This is causing me trouble in terms of error handling as this shouldn't be happening, instead, an error page should be shown.</p>
  
  <p>May I know if there is any solution or parameters which I can declare for the default redirection for each folder/page?</p>
  
  <hr>
  
  <p>P.S.</p>
  
  <p>I have a custom error tag which declare error pages with each error code attached.</p>
  
  <p>Is there an error code which I can use when a user enters a role-restricted website?</p>
  
  <p>E.g.</p>
  
  <pre><code>    &lt;customErrors defaultRedirect="~/Error/UnexpectedError.aspx" mode="On"&gt;
          &lt;error statusCode="404" redirect="~/Error/PageNotFound.aspx"/&gt;
      &lt;/customErrors&gt;
  </code></pre>
  
  <hr>
  
  <p>Solution for redirecting <strong>LOGGED IN</strong> User to error page:</p>
  
  <pre><code>    if (!Page.IsPostBack)
      {
  
          if (Request.IsAuthenticated &amp;&amp; !string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
  
              Response.Redirect("/App/Error/UnauthorizedAccess.aspx");
      }
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>How can I redirect a user to an error page if he/she is not logged in and tries to enter a role-restricted webpage?</p>