ASP.NET MVC 使用自定义角色提供程序重定向到拒绝访问的页面 [英] ASP.NET MVC redirect to an access denied page using a custom role provider

查看:22
本文介绍了ASP.NET MVC 使用自定义角色提供程序重定向到拒绝访问的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个自定义角色提供程序,并设置了一个 Authorize 属性来指定我的控制器中的一个角色,它工作得很好,如下所示:

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
...

但是当用户无权访问此控制器时,他会被重定向到登录页面.如何将他重定向到AcessDenied.aspx"页面?

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:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆