Asp.net mvc 如何在全局级别添加AuthorizeAttribute,并将其排除在某些操作之外?
我需要检查一个动作是否具有特定属性,我需要使用以下方法进行检查:Asp.net mvc 如何在全局级别添加AuthorizeAttribute,并将其排除在某些操作之外?,asp.net-mvc,asp.net-mvc-3,action-filter,Asp.net Mvc,Asp.net Mvc 3,Action Filter,我需要检查一个动作是否具有特定属性,我需要使用以下方法进行检查: protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext) { } 我知道我可以在这里查看: public override void OnAuthorization(AuthorizationContext filterContext) { filterContext.ActionDescriptor.IsDefined
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext) {
}
我知道我可以在这里查看:
public override void OnAuthorization(AuthorizationContext filterContext) {
filterContext.ActionDescriptor.IsDefined(typeof(AnonymousAllowedAttribute), true)
...
}
有人知道如何使用System.Web.HttpContextBase
对象获取ActionDescriptor
吗
更新
实际上,我想知道,如果标有
匿名AllowedAttribute
的任何操作返回true,或者如果可能的话,AuthorizeCore方法不运行(我指的是我的覆盖方法)。要做你想做的事,你需要在global.asax中编写并注册新的FilterProvider。
例如:
现在,如果您的任何操作未标记为[AnonymousAllowed]
应用程序,则认为它标记为[Authority]
PS:别忘了标记
[AnonymousAllowed]
您的登录和注册操作:)在哪里调用AuthorizeCore?@KirillBestemyanov所有方法都在公共类GlobalAuthorizeAttribute:AuthorizeAttribute中{
必须帮助您简短的回答是您无法从HttpContextBase获取ActionDescriptor。但是,如果您提供有关所需内容的更多详细信息,我可能会提供帮助。@KirillBestemyanov我更新问题您是否要使用GlobalAuthorizeAttribute标记所有方法,然后排除某些属性为AnonymousAllowedAt的方法贡品
public class AuthorizeFilterProvider:IFilterProvider
{
public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
if (!actionDescriptor.IsDefined(typeof(AnonymousAllowedAttribute), true))
return new Filter[] {new Filter(new AuthorizeAttribute(), FilterScope.Action, 0), };
return new Filter[0];
}
}
protected void Application_Start()
{
....
RegsterFilterProviders(FilterProviders.Providers);
}
private void RegsterFilterProviders(FilterProviderCollection providers)
{
providers.Add(new AuthorizeFilterProvider());
}