asp.net mvc授权属性重定向

moh*_*317 2 c# asp.net-mvc

在我的应用程序中,我已经制作了这样的自定义属性

public class AdminAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var isAuthorized = base.AuthorizeCore(httpContext);
        if (!isAuthorized || Auth.CurrentAdminUser == null)
        {
            return false;
        }
        else
        {
            return (SuperAdmin.Get(Auth.CurrentAdminUser.Id) != null) ? true : false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它工作正常,但我想要的是根据用户是否未登录进行重定向,然后登录页面,如果用户已登录但不是超级管理员,请将其取消授权页面.

现在发生的是所有未经授权的东西都通过web.config文件重定向到这个页面,

<authentication mode="Forms">
  <forms loginUrl="~/Site/NotAuthorize" timeout="2880" />
  <!-- this is where we can set up that if you are not authenticated, where should you go then?-->
</authentication> 
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激.

ohl*_*mar 7

你应该覆盖 HandleUnauthorizedRequest

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
    base.HandleUnauthorizedRequest(filterContext);

    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "xxx", action = "xxx", area = "" }));
}
Run Code Online (Sandbox Code Playgroud)