Custom Compile Time Attributes?

  • Thread starter Thread starter Chris Newby
  • Start date Start date
C

Chris Newby

I am trying to implment some business level user authorization in my current
..net 1.1 app. In C#, I would like to do something like:

[AuthorizationRule( "SomeRuleName" )]
public void MethodRequiringAuthorization()
{
... some code that requires authorization
}

But then have this code changed at compile time to something like:
public void MethodRequiringAuthorization()
{
if( Principal.Authorize( "SomeRuleName" ) )
{
... some code that requires authorization
}
}


Is there a way to do this? If not, is there some other way I can accomplish
the desired effect which is to decorate code with authorization rules
instead having to actually code it?

TIA//
 
It would have to be slightly different. It would have to be something like:

[AuthorizationRule("SomeRuleName")]
public void MethodRequiringAuthorization()
{
if (Principal.Authorize())
{
}
}

Principal.Authorize could then determine the attribute by walking the stack
and then using reflection to get the attribute from the calling method. But
at that point, you may as well pass it as a parameter as make it an
attribute. It would be faster than the stack-walk and reflection.

What it seems you really want to do is have some sort of trigger that the
method is called so that you can do the authorization when the method is
called. Unfortunately, there isn't a way, that I know of, to do that.

Pete
 
Chris said:
I am trying to implment some business level user authorization in my
current .net 1.1 app. In C#, I would like to do something like:

[AuthorizationRule( "SomeRuleName" )]
public void MethodRequiringAuthorization()
{
... some code that requires authorization
}

But then have this code changed at compile time to something like:
public void MethodRequiringAuthorization()
{
if( Principal.Authorize( "SomeRuleName" ) )
{
... some code that requires authorization
}
}

Sure, all you have to do is write your own version of the C# compiler.
There is source code for one in the SSCLI (Rotor) download.
Is there a way to do this? If not, is there some other way I can
accomplish the desired effect which is to decorate code with
authorization rules instead having to actually code it?

Seriously, the only way that you can write an attribute that will be
acted upon by the compiler is to write the compiler.

You could derive the class from an abstract class that has a method that
tests for custom attributes and performs the authorization check you
specify, although clearly you will have to call this method in your
method:

// in base class
bool Authorized()
{
StackTrace st = new StackTrace();
StackFrame f;
f = st.GetFrame(st.FrameCount - 1); // to get the frame of the method
that called this
MethodBase mb = f.GetMethod();
object[] attrs = mb.GetCustomAttributes(false);
foreach(obj attr in attrs)
{
AuthorizationRuleAttribute ar = attr as
AuthorizationRuleAttribute;
if (ar != null)
{
// access the rule name and perform the check
return DoCheckOnAttributeRule(ar);
}
}
return false;
}

[AuthorizationRule( "SomeRuleName" )]
public void MethodRequiringAuthorization()
{
if (Authorized())
{
// do stuff
}
}


Richard
 
Back
Top