這是我CustomAuthorizeAttribute類:爲某些Action MVC添加相同的AuthorizeAttribute時,如何覆蓋全局AuthorizeAttribute?
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public string ControllerName { get; set; }
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (ControllerName != "pass")
{
// stop or redirect
}
}
}
我把它註冊到全局過濾器的所有控制器可以使用:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new AdminAuthorizeAttribute());
}
對於一些具體的行動我與帕拉姆ControllerName添加:
[AdminAuthorize(ControllerName="pass")]
public ActionResult Index()
{
return View();
}
但問題是現在在OnAuthorization(),ControllerName總是得到null執行特定的Action時。
難道是因爲我不能使用全局的authorizeAttribute和相同的attibute對於一些特定的動作在一起?爲什麼?我一直認爲如果我爲特定的Action添加一些AuthorizeAttribute,並將Attribute添加到全局過濾器,則特定的Action將獲得高度優先。
UPDATE1:
如果問題來源是2授權的所有執行。那麼我怎麼覆蓋全球授權過濾器當我添加一個相同的AuthorizeAttribute某些行動? (只不同的是帕拉姆,我只是希望它忽略了全球授權當我添加一個對一些行動)
您可以手動更改該屬性的Order屬性,以使其成爲fir先編輯。 – 2014-11-01 13:16:39