ASP.NET MVC重定向到一个拒绝访问页面使用自定义角色提供 [英] ASP.NET MVC redirect to an access denied page using a custom role provider
本文介绍了ASP.NET MVC重定向到一个拒绝访问页面使用自定义角色提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建一个自定义角色的供应者,我设置了授权属性在我的控制器指定一个角色,它的工作就好了,像这样的:
[授权(角色=超级管理员)]
公共类SuperAdminController:控制器
...
但是,当一个用户好好尝试访问该控制器,他重定向到登录页面。
我怎样才能重定向他一个AcessDenied.aspx页?
解决方案
[AccessDeniedAuthorize(角色=超级管理员)]
公共类SuperAdminController:控制器
AccessDeniedAuthorizeAttribute.cs:
公共类AccessDeniedAuthorizeAttribute:AuthorizeAttribute
{
公共覆盖无效OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext); 如果(filterContext.Result是HttpUnauthorizedResult)
{
filterContext.Result =新RedirectResult(〜/ AcessDenied.aspx);
}
}
}
I'm creating a custom role provider and I set a Authorize attribute specifying a role in my controller and it's working just fine, like this:
[Authorize(Roles="SuperAdmin")]
public class SuperAdminController : Controller
...
But when an user doens't have access to this controller, he's redirected to login page. How can I redirect him to a "AcessDenied.aspx" page?
解决方案
[AccessDeniedAuthorize(Roles="SuperAdmin")]
public class SuperAdminController : Controller
AccessDeniedAuthorizeAttribute.cs:
public class AccessDeniedAuthorizeAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if(filterContext.Result is HttpUnauthorizedResult)
{
filterContext.Result = new RedirectResult("~/AcessDenied.aspx");
}
}
}
这篇关于ASP.NET MVC重定向到一个拒绝访问页面使用自定义角色提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文