怎样才能让已经应用到一个特定的控制器动作授权过滤器列表? [英] How does one get a list of authorization filters that have been applied to a particular controller action?

查看:190
本文介绍了怎样才能让已经应用到一个特定的控制器动作授权过滤器列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个 asp.net MVC应用程序后,一位同事正在努力建立一个角色依赖集网站的布局控制UI元素和我的同事愿意做出出现HTML扩展或不根据用户的角色关系

In an asp.net MVC application, a colleague is trying to build a role dependent set of UI elements in the web site's layout control, and my colleague wants to make an html extension that appears or doesn't based on the user's role relationship.

的同事希望能够测试的特定链路链接到动作并检查是否该用户被授权,即使访问该链接。为了做到这一点,将是不错的运行所有针对控制器动作授权过滤器。

The colleague wants to be able to test the action that a particular link is linking to and check to see if the user is authorized to even visit that link. In order to do this it would be nice to run all of the authorization filters against the controller action.

为了做到这一点必须检索所有为特定动作注册授权滤波器

In order to do this one must retrieve all of the authorization filters registered for a particular action.

有谁知道如何抓住刚授权过滤与特定的控制器操作相关的属性?

Does anyone know how to grab just the authorization filters attributes associated with a particular controller action?

推荐答案

像这样

typeof(MyController).GetType()
.GetMethod("action")
.GetCustomAttributes(true)
.Where(attr=>attr is IAuthorizationFilter).Cast<IAuthorizationFilter>()

但你必须伪造filtercontext。就个人而言,我会从自身pcisely $ P $用于测试目的的分离过滤器过滤功能。所以,你得这将被过滤器称为实际authorizator'对象。

But you'll have to fake the filtercontext. Personally, I'd separate the filter functionality from the filter itself precisely for testing purposes. So you'd have the actual 'authorizator' object which would be called by the filter.

这篇关于怎样才能让已经应用到一个特定的控制器动作授权过滤器列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆