【发布时间】: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");
}
}
}
【问题讨论】:
-
因为
<allow users="*"/>表示允许任何用户访问,你必须允许特定的组或用户。 -
是的,由于某种原因,某些 web.config 没有显示出来。
-
Propmt表示黄色错误页面对吗?? -
你的代码解决了这个问题吗?我希望如此,如果不尝试下面的答案。
-
您使用的是哪个版本的
IIS?
标签: c# asp.net authentication web active-directory