重定向未授权用户asp net

mad*_*ads 12 c# asp.net authentication active-directory web

我正在asp.net上创建一个简单的网站.我想限制对侧的访问,以便只允许特定AD组中的用户.我已经做到了,它工作正常.但是,当不在AD组中的用户尝试访问该站点时,他们会收到登录提示.如何将未经授权的用户重定向到自定义页面,而不是他们获取登录提示?

下面是我的web.config.代码的最低部分是我尝试过但没有用的东西.

<configuration>
<system.web>
  <compilation debug="true" targetFramework="4.0" />
  <authentication mode="Windows"/>
  <authorization>
    <allow roles="DOMAIN\GROUP"/>
    <deny users="*"/>
  </authorization>
</system.web>

<location path="AccessDenied.aspx">
<system.web>
<authorization>
  <allow users="*"/>
</authorization>
</system.web>
</location>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我已将此添加到Global.asax.cs:

protected void Application_EndRequest(Object sender, EventArgs e)
    {
        if (HttpContext.Current.Response.Status.StartsWith("401"))
            {
                HttpContext.Current.Response.ClearContent();
                Server.Execute("AccessDenied.aspx");
            }
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗 ?

编辑: 我尝试了一些已发布的解决方案,但它们没有用. 但是我使用了这段代码:

void Application_EndRequest(object sender, System.EventArgs e)
    {
        if (((Response.StatusCode == 401)
        && (Request.IsAuthenticated == true)))
        {
            Response.ClearContent();
            Response.Redirect("~/AccessDenied.aspx");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*ren 3

您可以使用Response.RedirectServer.Transfer

Response.Redirect("AccessDenied.aspx");
Run Code Online (Sandbox Code Playgroud)

完整示例:

protected void Application_EndRequest(Object sender, EventArgs e)
{
  if (HttpContext.Current.Response.Status.StartsWith("401"))
  {
      HttpContext.Current.Response.ClearContent();
      Response.Redirect("AccessDenied.aspx");
  }
}
Run Code Online (Sandbox Code Playgroud)