c# - How to get the decorator objects from inside the decorated method -


in asp.net mvc 4 controller class have actions decorate customauthorize attribute access restricted roles.

i roles inside action methods , need customauthorize attribute method decorated with.

how do this?

sample code of trying below:

public class testcontroller : controller {     // users in viewer , admin roles should     [customauthorize("viewer", "admin")]     public actionresult index()     {         //need customauthorizeattribute associated roles     } } 

customauthorizeattribute subclass of system.web.mvc.authorizeattribute.

if want attribute method can this, example reflection:

var atrb = typeof(testcontroller).getmethod("index")              .getcustomattributes(typeof(customauthorizeattribute), true)              .firstordefault() customauthorizeattribute; 

or current method;

var atrbcurrentmethod = system.reflection.methodbase.getcurrentmethod()                 .getcustomattributes(typeof(customauthorizeattribute), true)                 .firstordefault() customauthorizeattribute; 

or more flexible way, if want later create function, stated in comment:

public customauthorizeattribute getcustomauthorizeattribute() {             return new stacktrace().getframe(1).getmethod()                 .getcustomattributes(typeof(customauthorizeattribute), true).firstordefault() customauthorizeattribute;         } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -