c# - Get the Class name in a class with atribute inside a attribute method -
i'm doing custom authorize attribute.
want use attribute in class , controller level.
this code work on method level
public class customauthorizeattribute : authorizeattribute { public customauthorizeattribute([callermembername] string callername = null) { callername = callername; } public string callername { get; set; } protected override bool authorizecore(httpcontextbase httpcontext) { if (httpcontext.user.isinrole("admin")) return true; ... if (callername == something) { } ... return base.authorizecore(httpcontext); } }
like that
[customauthorize()] public actionresult index() { return view(); }
but want use globally in controller
[customauthorize()] public class usuarioscontroller : controller { salusdbcontext db = new salusdbcontext(); // get: usuarios public actionresult index() { return view(); } }
sorry bad english.
in end didn't need reflection or fancy, in authorizeattribute have httpcontext, can route, controller , action name.
public class customauthorizeattribute : authorizeattribute { protected override bool authorizecore(httpcontextbase httpcontext) { var routevalues = httpcontext.request.requestcontext.routedata.values; if (httpcontext.user.isinrole("admin")) return true; var controller = routevalues["controller"]; var action = routevalues["action"]; return base.authorizecore(httpcontext); } }
Comments
Post a Comment