我试图以声明的方式使用基于角色的授权,当未经授权的用户试图访问页面时,它永远不会触发异常或向用户显示错误消息。我应该怎么做才能显示未经授权的消息?这可以以声明的方式实现吗?
使用编码不是一个很好的选项,我有几个角色,文件夹授权给几个角色,而其他文件夹授权给一个角色。
谢谢
发布于 2010-09-02 04:47:30
使用登录页中的以下代码将用户重定向到未经授权的页或默认页。
protected void Page_Load( object sender, EventArgs e )
{
if( Page.IsPostBack )
return;
if( !Request.IsAuthenticated )
return;
if( !string.IsNullOrEmpty( Request.QueryString["ReturnUrl"] ) && !UrlAuthorizationModule.CheckUrlAccessForPrincipal(Request.QueryString["ReturnUrl"], User,"GET"))
{
// In Forms Authentication, authenticated but unauthorized requests are converted into a Redirect to the Login page.
// Redirect these to an error page instead.
Response.Redirect( "~/UnauthorizedAccess.aspx", false );
}
else
{
Response.Redirect( FormsAuthentication.DefaultUrl, false );
}
}有关正在发生的事情的图片和更多信息,请参阅此链接:
http://www.asp.net/security/tutorials/user-based-authorization-cs
发布于 2010-08-06 23:46:21
如果授权失败,它将抛出异常。它一定是过去了。您使用什么进行身份验证?您是否已禁用匿名访问?
https://stackoverflow.com/questions/3424498
复制相似问题