Wednesday 15 May 2013

c# - How add custom AuthorizeAttribute for some specific Controller use code on MVC? -



c# - How add custom AuthorizeAttribute for some specific Controller use code on MVC? -

the general case set attribute on contoller top. like:

[authorize] public class xxcontroller : controller { }

but because project need utilize 3rd part binary dll. we don't have source code alter original controller on binary dll, can ignore default authorize utilize setting it, want know there possible add together custom authorize attribute xxcontroller use code?

this customauthorizeattribute:

public class customauthorizeattribute : authorizeattribute { public bool isonlyadminaccess {get ;set;} public override void onauthorization(authorizationcontext filtercontext) { if (session.isadmin && !isonlyadminaccess ) { // redirect } } }

so think there maybe on global.aspx. utilize way like(just idea) add together customauthorizeattribute specific controller:

addcustomattributewayormethod(xxcontroller, new customauthorizeattribute(isonlyadminaccess = true));

some friend maybe said add together globalfiltercollection , like:

globalfiltercollection.add(new customauthorizeattribute ());

but already add together global controller. need xxcontroller utilize isonlyadminaccess = true property, other controller don't need that.

if you, create subclass of original controller , set attribute on kid class.

// other controller in dll, don't have source code public controller theircontroller { // , yours, have in code // custom attribute [customattribute] public controller yourcontroller : theircontroller { // actions inherited

c# asp.net .net asp.net-mvc

No comments:

Post a Comment