![](/img/trans.png)
[英]How override the global AuthorizeAttribute when add same AuthorizeAttribute for some Action MVC?
[英]How add custom AuthorizeAttribute for some specific Controller use code on MVC?
一般情况下,我们只是将属性放在Contoller顶部 。 喜欢:
[Authorize]
public class XXController : Controller
{
}
但是由于我的项目需要使用第三方二进制DLL。 我们没有用于在二进制dll上更改原始控制器的源代码 ,但是我们可以忽略默认的Authorize使用某些设置,那么我只想知道是否可以为XXController 使用添加自定义Authorize属性代码 ?
这是我的CustomAuthorizeAttribute:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public bool IsOnlyAdminAccess {get ;set;}
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (Session.IsAdmin && !IsOnlyAdminAccess )
{
// redirect
}
}
}
所以现在我觉得有没有可能在global.aspx。 使用某种方式( 只是idea )为特定的Controller添加CustomAuthorizeAttribute:
AddCustomAttributeWayOrMethod(XXController, new CustomAuthorizeAttribute(IsOnlyAdminAccess = true));
也许有些朋友说我只是将其添加到GlobalFilterCollection
,例如:
GlobalFilterCollection.Add(new CustomAuthorizeAttribute ());
但是我已经为全局控制器添加了它。 我只需要我的XXController使用IsOnlyAdminAccess = true属性,其他控制器都不需要。
如果您是我,则将创建原始控制器的子类,并将属性放在子类上。
// this is the other controller in a dll, you don't have the source code
public controller TheirController {
// and this is yours, you have it in your code
// with your custom attribute
[CustomAttribute]
public controller YourController : TheirController {
// all actions are inherited
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.