加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 如何在全局级别添加AuthorizeAttribute并将其排

发布时间:2020-12-16 09:51:47 所属栏目:asp.Net 来源:网络整理
导读:我需要检查某个操作是否具有特定属性,我需要在以下方法中执行此操作: protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext) {} 我知道我可以在这里查看: public override void OnAuthorization(AuthorizationContext filterCon
我需要检查某个操作是否具有特定属性,我需要在以下方法中执行此操作:

protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext) {

}

我知道我可以在这里查看:

public override void OnAuthorization(AuthorizationContext filterContext) {

    filterContext.ActionDescriptor.IsDefined(typeof(AnonymousAllowedAttribute),true)
 ...
}

有没有人知道如何使用System.Web.HttpContextBase对象获取ActionDescriptor?

UPDATE

实际上我想要用AnonymousAllowedAttribute标记的任何操作AuthorizeCore方法返回true或者如果可能的话不运行(我的意思是我的覆盖方法).

解决方法

要做你想做的事,你需要在global.asax中编写和注册新的FilterProvider.
例:

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];
    }
}

Global.asax中:

protected void Application_Start()
    {
        ....
        RegsterFilterProviders(FilterProviders.Providers);
    }

    private void RegsterFilterProviders(FilterProviderCollection providers)
    {
        providers.Add(new AuthorizeFilterProvider());
    }

现在,如果您的任何操作未标记[AnonymousAllowed]应用程序,则认为它被标记为[授权]PS:不要忘记在[AnonymousAllowed]上标记您的登录并注册操作:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读