It is possible to create an attributed security model like you suggest but it is not easy. Your securable objects have to inherit from ContextBoundObject and your security attribute from ContextAttribute (or implement the IContextAttribute interface). Then:
- Implement the IContextAttribute.GetPropertiesForNewContext(IConstructionCallMessage) method. You create an object, call it
MySecurityProperty
, that implements the IContextProperty
and IContributeObjectSink
interfaces and add it to the IConstructionCallMessage.ContextProperties
collection.
- In the implementation of MySecurityProperty.GetObjectSink(MarshalByRefObject, IMessageSink) construct an object, call it
MySecurityAspect
, that implements the IMessageSink interface.
- In the implementation of MySecurityAspect.SyncProcessMessage(IMessage), you actually check the call to see whether it is valid. If it casts to IMethodMessage, indicating a method call, you can query the properties of
IMethodMessage
to see whether it is calling a method or class with your security attribute and do the appropriate checks. If the call is unauthorized, throw an exception of the appropriate type.
It will take you a few hours to get it working but, once it does, it makes sense. It is just a very undersupported part of the .Net framework. The big problem, beyond the complexity, is that it forces your securable classes to inherit from ContextBoundObject rather than any other library classes. ContextBoundObject also inherits from MarshalByRef, which can interfere with serialization.
See http://www.developerfusion.com/article/5307/aspect-oriented-programming-using-net/3/ for a more in depth explanation.