473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Custom Compile Time Attributes?

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//
Nov 17 '05 #1
2 5463
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 Newby" <ch*********@rockcreekglobal.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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//

Nov 17 '05 #2
Chris Newby wrote:
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
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Steve Jorgensen | last post by:
Here is some code to generate code for raising and getting information about custom errors in an application. Executing the GenerateXyzErrDefs procedure generates the code. <tblXyzError>...
8
by: Martin Lapierre | last post by:
I try to make a custom CodeAccessSecurityAttribute, but hit the wall at every corner. I created a simple custom security attribute, which is working (see below). But... 1) I have to put the...
1
by: Bret Pehrson | last post by:
I've converted a non-trivial C++ library to managed, and get the following unhelpful linker error: Assignment.obj : error LNK2022: metadata operation failed (80131195) : Custom attributes are...
4
by: Wiktor Zychla | last post by:
Hello, for some reason I need to use attributes in ILAsm code and read these attributes in C# code. My problem is with the attribute declaration inside ILAsm code. I know that I define any...
2
by: Glen | last post by:
I'm working on a custom assembly and I'm trying to figure out the best approach to handling known constraints within the assembly, once compiled, to alert the developer at compile time of a...
3
by: Edward Diener | last post by:
I understand the syntax of custom attributes, but I have no idea what they are supposed to do. Anyone care to give me a clue as to their functionality ?
3
by: Mark R. Dawson | last post by:
Hi all, I am trying to get custom attributes from a property. I can do this if I pass in the name of the property i.e. "Name" to the reflection methods, but if I pass in set_Name which is what...
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.