重定向未经授权的用户 asp net

本文关键字:用户 asp net 授权 重定向 | 更新日期: 2023-09-27 18:30:27

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

下面是我的网络配置。代码的最低部分是我尝试过但没有奏效的东西。

<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>

我已将其添加到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");
            }
}

有什么想法吗?

编辑:我尝试了一些发布的解决方案,但它们不起作用。但是我用这段代码得到了它:

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

重定向未经授权的用户 asp net

您可以使用

Response.RedirectServer.Transfer

Response.Redirect("AccessDenied.aspx");

完整示例:

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

假设您要处理所有"未经授权"错误:

<customErrors defaultRedirect="Error.aspx" mode="On">
    <error statusCode="401" redirect="Unauthorized.aspx" />
    <error statusCode="403" redirect="Forbidden.aspx" />
</customErrors>

任何 401(未经授权)请求都将转发到 Unauthorized.aspx。

我在这方面

取得了更大的成功:

      // This is a workaround for the fact that we are not using MVC and its attributes
      // This is the situation where a user is logged in - but not authorized for this page
      void Application_EndRequest (object sender, System.EventArgs e)
      {
         if (((Response.StatusCode == 302) && (Request.IsAuthenticated == true)))
         {
            try
            {
               string sLoc = Response.Headers ["Location"];
               if (sLoc.Contains ("Login"))
               {
                  Response.ClearContent ();
                  Response.Redirect ("~/AccessDenied.aspx");
               }
            }
            catch
            {
            }
         }
      }
<authorization>
 <!--<allow users="*"/>-->This here means allow everyone .
 <allow users="AD"/> -- Add this group to AD domain .  
 <deny users="?"/> --Deny unknown users(Not authenticated)
 <allow roles="Admins"/> --If you have created roles .    

如果您有本地组,请使用<allow user ="AD">但您必须将其注册到 AD 域。 <allow roles ="AD" />仅适用于域 AD 组,不适用于本地组。

 protected void Application_EndRequest(Object sender,EventArgs e) 
  { 
     HttpContext context = HttpContext.Current;
     if (context.Response.Status.Substring(0,3).Equals("401"))
     {
        context.Response.ClearContent();
         //do redirect here 
     } 
  }