简体   繁体   English

如何从装饰方法内部获取装饰对象

[英]How to get the decorator objects from inside the decorated method

In my ASP.NET MVC 4 controller class I have actions that I decorate with CustomAuthorize attribute so the access is restricted to some roles. 在我的ASP.NET MVC 4控制器类中,我具有用CustomAuthorize属性修饰的操作,因此,访问权限仅限于某些角色。

I would like to get the roles from inside the action methods and for this I need the CustomAuthorize attribute that the method is decorated with. 我想从动作方法内部获取角色,为此,我需要用该方法装饰的CustomAuthorize属性。

How do I do this? 我该怎么做呢?

Sample code of what I am trying to do is below: 我尝试做的示例代码如下:

public class TestController : Controller
{
    // Only the users in viewer and admin roles should do this
    [CustomAuthorize("viewer", "admin")]
    public ActionResult Index()
    {
        //Need CustomAuthorizeAttribute so I get the associated roles
    }
}

CustomAuthorizeAttribute is a subclass of System.Web.Mvc.AuthorizeAttribute . CustomAuthorizeAttributeSystem.Web.Mvc.AuthorizeAttribute的子类。

Attributes are not per instance, they are per class so there is no "current" instance of the CustomAuthorizeAttribute. 属性不是按实例,而是按类,因此CustomAuthorizeAttribute没有“当前”实例。 See the documentation on attributes. 请参阅有关属性的文档。

https://msdn.microsoft.com/en-us/library/z0w1kczw.aspx https://msdn.microsoft.com/en-us/library/z0w1kczw.aspx

If you need to get the CustomAuthorizeAttribute you can use reflection to get information about the class you're in and then pull the properties of the attribute but I would question why you need to. 如果需要获取CustomAuthorizeAttribute,则可以使用反射来获取有关您所在类的信息,然后提取该属性的属性,但是我会问为什么需要这样做。 Is there something specific you want that we can help more with? 您是否需要特定的东西,我们可以提供更多帮助?

if you want to get attribute from that method you can do this, for example with reflection: 如果要从该方法获取属性,则可以执行此操作,例如,使用反射:

var atrb = typeof(TestController).GetMethod("Index")
             .GetCustomAttributes(typeof(CustomAuthorizeAttribute), true)
             .FirstOrDefault() as CustomAuthorizeAttribute;

or for the current method; 或对于当前方法;

var atrbCurrentMethod = System.Reflection.MethodBase.GetCurrentMethod()
                .GetCustomAttributes(typeof(CustomAuthorizeAttribute), true)
                .FirstOrDefault() as CustomAuthorizeAttribute;

or a more flexible way, if you want to later create a function, as you stated in your comment: 或更灵活的方式,如果您以后想要创建函数,如注释中所述:

public CustomAuthorizeAttribute GetCustomAuthorizeAttribute() {
            return new StackTrace().GetFrame(1).GetMethod()
                .GetCustomAttributes(typeof(CustomAuthorizeAttribute), true).FirstOrDefault() as CustomAuthorizeAttribute;
        }

Why not use Roles.GetRolesForUser(); 为什么不使用Roles.GetRolesForUser(); method to get all Roles user is having ? 方法来获取用户拥有的所有角色? This should have the same results as you would get from Reflection parsing the attributes value. 这应该具有与从Reflection解析属性值中获得的结果相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM