title: asp.net core 3.1 unauthorized 401 redirect to an unauthorized razorpage not working
body: <p>I am new to .net core but have developed in asp.net for a few years. I have a site in core that is Windows AD based auth. I am using AD groups to control access, my razor index page is setup for an AD group</p>
  <pre><code>[Authorize(Policy = &quot;ADRoleOnly&quot;)]
  </code></pre>
  <p>when a user that is not part of the group tries to access the razor index page they get a 403 denied. I am trying to figure out how to redirect them to a razor page i have setup as &quot;/unAuthAccess&quot;  . This &quot;/unAuthAccess&quot; razorpage is set to</p>
  <pre><code>[AllowAnonymous]
  </code></pre>
  <p>and i verified that users in the AD group can view this '/unAuthAccess&quot; page fine, as well as users that are not part of the AD group that get the 403, so the allowanonymous is working for the /unAuthAccess page.</p>
  <p>so i don't know where or HOW to put in a redirect when any users hits any page i have set with the policy when they don't have authorization to view it.</p>
  <pre><code>[Authorize(Policy = &quot;ADRoleOnly&quot;)]
  </code></pre>
  <p>I have tried in Startup.cs the following with both 401 and 403 status</p>
  <pre><code>app.UseStatusCodePages(async context =&gt;
  {
      if (context.HttpContext.Response.StatusCode == 401)
      {
           app.Use(async (context, next) =&gt;
           {
                 context.Request.Path = &quot;/UnAuthAccess&quot;;
                 await next();
           });
      }
   });
  </code></pre>
  <p>which did not work. so then i tried</p>
  <pre><code> app.UseStatusCodePagesWithRedirects(&quot;/Errors/{0}&quot;);
  </code></pre>
  <p>with the razor page set to &quot;/Errors/401&quot; and also &quot;/Errors/403&quot; i couldn't remember which one it should be set to and catch the unAuth access from the user.</p>
  <p>either way the user doesn't get redirected to my razorpage and i just get a standard browser HTTP error 403 denied message.</p>
  <p>just looking for help on how redirects work in core??? should i be doing the redirect somehow directly from the razor page cs/backend code if so how??</p>
  <p>I have this working in old asp.net 4.x using Global.asax, but this method does not work in core :(</p>
  <pre><code>if (Response.StatusCode == 401 &amp;&amp; Request.IsAuthenticated)
              {
                  Response.Redirect(&quot;~/unauthAccess.aspx&quot;);
              }
  </code></pre>