Reputation: 10992
I want to make custom attributes on classes to restrict who calls them.
For example:
[Permission("Admin")]
public class MyData {
....
How to do that? So I want to really make some sort of Permission-class.
Upvotes: 3
Views: 4078
Reputation: 19305
You either use the ones that already exist, or you write your own and hook them into the Code Access Security architecture. This has been around for a really long time, and I was able to find these articles:
Creating Your Own Code Access Permissions
Cut-away from the link above:
Implementing a custom code access permission involves the following steps, some of which are optional. Each step is described in a separate topic.
- Design the
Permission
class.- Implement the
IPermission
andIUnrestrictedPermission
interfaces.- Implement the
ISerializable
interface, if necessary for performance or to support special data types.- Handle XML encoding and decoding.
- Add support for declarative security, by implementing an
Attribute
class.- Demand custom permission for your permission, where appropriate.
It's really steps 1, 2, 5 and 6 that are of interest to you.
Upvotes: 4
Reputation: 7591
Permission would inherit from Attribute. Then you would need to implement code to inspect the class for these attributes before calling the class/method. the .net framework already has this functionality built in. SecurityPermissionAttribute and FileIOPermissionAttribute are two implementations you may want to consider before rolling your own.
Upvotes: 0