【问题标题】:Redirect unauthorized users asp net重定向未经授权的用户 asp net
【发布时间】:2013-09-10 06:26:11
【问题描述】:

我正在使用 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>

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

【问题讨论】:

  • 因为&lt;allow users="*"/&gt;表示允许任何用户访问,你必须允许特定的组或用户。
  • 是的,由于某种原因,某些 web.config 没有显示出来。
  • Propmt 表示黄色错误页面对吗??
  • 你的代码解决了这个问题吗?我希望如此,如果不尝试下面的答案。
  • 您使用的是哪个版本的IIS

标签: c# asp.net authentication web active-directory


【解决方案1】:

您可以使用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");
  }
}

【讨论】:

    【解决方案2】:

    假设您要处理所有“未经授权”的错误:

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

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

    【讨论】:

    【解决方案3】:

    我在这方面取得了更大的成功:

          // 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
                {
                }
             }
          }
    

    【讨论】:

      【解决方案4】:
      <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 .    
      

      如果您有本地组而不是使用&lt;allow user ="AD"&gt;,但您必须将其注册到 AD 域。 &lt;allow roles ="AD" /&gt; 仅适用于域 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 
           } 
        }
      

      【讨论】:

      • allow user 用于用户帐户名称,而不是 AD 组。
      • @Nicholas 如果您可以查看此声明 If you have local group than use &lt;allow user ="AD"&gt; but you have to register it to AD domain . &lt;allow roles ="AD" /&gt; will work only with Domain AD groups not for local groups . 此声明可能会令人困惑并引导读者朝另一个方向发展allow user is for Active Directory .
      • 如果您使用allow roles 并将角色管理器设置为WindowsTokenRoleProvider,如果服务器是域的成员,它将检测本地计算机组和域安全组。见msdn.microsoft.com/en-us/library/wce3kxhd%28v=vs.100%29.aspx
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 2012-07-03
      • 2015-11-27
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      相关资源
      最近更新 更多