Asp.net 当用户的角色权限不足时,将其重定向到web.config中的另一个页面
我正在使用ASP.NET,我希望能够将用户从web配置重定向到另一个页面 我有很多限制,比如:Asp.net 当用户的角色权限不足时,将其重定向到web.config中的另一个页面,asp.net,redirect,web-config,asp.net-membership,Asp.net,Redirect,Web Config,Asp.net Membership,我正在使用ASP.NET,我希望能够将用户从web配置重定向到另一个页面 我有很多限制,比如: <location path="Structures.aspx"> <system.web> <authorization> <allow roles="Admin"/> <deny users="*" /> </authorization> </sy
<location path="Structures.aspx">
<system.web>
<authorization>
<allow roles="Admin"/>
<deny users="*" />
</authorization>
</system.web>
</location>
但这不是我想要的
我需要在web.config中完成,而不是在代码隐藏中。
谢谢 假设要处理所有“未经授权”的错误:
我注意到我的应用程序正在使用设置了“位置”标题的“302找到”代码重定向回登录页面。因为我的登录页面恰好位于共享同一服务器的外部应用程序中,所以我无法修改它
相反,我将其添加到了我的global.asax:
protected void Application_EndRequest(Object sender, EventArgs e)
{
if (Response.Status.StartsWith("302")
&&
Request.IsAuthenticated
&&
Response.RedirectLocation.StartsWith(System.Web.Security.FormsAuthentication.LoginUrl))
{
//log.Trace("Preventing redirection from app to login form since user is already logged in. It's authorization issue, not authentication.");
Response.Clear();
Response.Redirect("~/AccessDenied.aspx");
}
}
谢谢,我需要很多角色来管理使用代码隐藏方法。每个页面都有它自己的角色(它接受)customError resolution看起来更好。我将尝试添加customErrors,幸运的是,在我的情况下,添加customErrors对我没有帮助,它将错误重定向到error.aspx,但在不允许用户按其角色查看页面的情况下,它仍将我重定向到Default.aspx,例如,响应为:HTTP/1.1 302 Found Location:/Default.aspx?ReturnUrl=%2fStructures.aspx
// base class
public class AdminOnlyPage : Page
{
/*...*/ Page_Load(Object sender, EventArgs e)
{
/* check if the user is admin otherwise reject and redirect */
}
}
// Your "Structures.aspx" page
public class Structures : AdminOnlyPage
{
}
protected void Application_EndRequest(Object sender, EventArgs e)
{
if (Response.Status.StartsWith("302")
&&
Request.IsAuthenticated
&&
Response.RedirectLocation.StartsWith(System.Web.Security.FormsAuthentication.LoginUrl))
{
//log.Trace("Preventing redirection from app to login form since user is already logged in. It's authorization issue, not authentication.");
Response.Clear();
Response.Redirect("~/AccessDenied.aspx");
}
}