473,566 Members | 3,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:

[AuthorizationRu le( "SomeRuleNa me" )]
public void MethodRequiring Authorization()
{
... some code that requires authorization
}

But then have this code changed at compile time to something like:
public void MethodRequiring Authorization()
{
if( Principal.Autho rize( "SomeRuleNa me" ) )
{
... 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 5486
It would have to be slightly different. It would have to be something like:

[AuthorizationRu le("SomeRuleNam e")]
public void MethodRequiring Authorization()
{
if (Principal.Auth orize())
{
}
}

Principal.Autho rize 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*********@ro ckcreekglobal.c om> wrote in message
news:%2******** ********@TK2MSF TNGP12.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:

[AuthorizationRu le( "SomeRuleNa me" )]
public void MethodRequiring Authorization()
{
... some code that requires authorization
}

But then have this code changed at compile time to something like:
public void MethodRequiring Authorization()
{
if( Principal.Autho rize( "SomeRuleNa me" ) )
{
... 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:

[AuthorizationRu le( "SomeRuleNa me" )]
public void MethodRequiring Authorization()
{
... some code that requires authorization
}

But then have this code changed at compile time to something like:
public void MethodRequiring Authorization()
{
if( Principal.Autho rize( "SomeRuleNa me" ) )
{
... 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.GetCustomAtt ributes(false);
foreach(obj attr in attrs)
{
AuthorizationRu leAttribute ar = attr as
AuthorizationRu leAttribute;
if (ar != null)
{
// access the rule name and perform the check
return DoCheckOnAttrib uteRule(ar);
}
}
return false;
}

[AuthorizationRu le( "SomeRuleNa me" )]
public void MethodRequiring Authorization()
{
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
2697
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> ErrorCodeOffset (Long Int) ErrorObjBaseName (Text(50)) ErrorDescription (Text(255)) 1 DontDoThat Hey! Don't do that!
8
5014
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 assembly in "C:\Program Files\Microsoft Visual Studio ..NET 2003\Common7\IDE": shoudn't it be the application's directory instead? 2) I'm unable to...
1
2135
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 not consistent: (0x0c0001a5). Display.obj : error LNK2022: metadata operation failed (80131195) : Custom attributes are not consistent: (0x0c000108)....
4
2723
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 attribute by using ILasm's ".custom" keyword but I do not understand how to initialize the attribute Value blob when the attribute contains some fields....
2
4620
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 potential issue. For example, in the assembly I would like to add a constraint that states a particular property member of the class can not be equal to...
3
2136
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
3178
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 the set piece of the Name property gets compiled to, which I am getting from the stack trace, then the attributes are not returned. For example,...
2
2514
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 is the usage of "Custom Attribute" in real life project. Can somebody please help me understand where are these informations really helpful in...
15
6491
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 accept other controls. The control i drag drop on it becomes the child of my custom control's parent form and not the child of my custom control. Then...
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.