Extending the security system with new permissions...
I'm banging my head against a wall right now trying to extend the CAS system with my own permissions. I've figured on doing this in three steps. First, by figuring out what needs to go into a new permission. This wasn't hard at all, and consisted of creating a new attribute for declarative security and a class to back that attribute up for run-time security. Compile that guy up and on to step 2.
Step 2 is even easier, just create a class (this doesn't really matter that much), and have the assembly use the new permissions attribute. I placed it as a SecurityAction.RequestMinimum. Figured that would be the best place to test the new permission since now I can control whether or not assemblies load based on the new permissions they are going to require (the new permissions by the way are things like StaticConstructor, UseStatics, etc... for the plug-in library). Compile this guy up and on to step 3.
Now, I figured if I have full-trust, running from My Computer, that the assembly should just load and run. Not the case. I can't seem to get past this point since the requested permission can't be assigned by the security system. Is something going wrong here, is CAS not as extensible as I thought? I went ahead and explicitly created a new run-time security policy that contained my new permission, verified that the permission had been applied, set it on the current AppDomain and let the code run again. Same error.
So now I'm curious and at a big road-block. Have I properly coded the CAS permission and attribute to begin with? I think so. The IL compared to a comparable permission (I used DnsPermission since it has a None and an Unrestricted state and that is it), is 100% the same when doing name replacement. Applying the permission as an assembly level attribute shouldn't be a big deal, or at least I didn't think it would be, and the attribute's IL also matches perfectly with the DnsPermission as does the XML serialization that shows up in the metadata. So why in the heck can't the permission be granted under the security system? Hell, just to check, I added DnsPermission as a required assembly attribute and it worked flawlessly. Do CAS permissions have to be located in strongly named assemblies (I have to admit, I just thought of this and haven't tried it yet)?
I'm not willing to give this one up yet. I'm definitely going to keep playing with the stuff. Imagine a plug-in system where assemblies are dynamically loaded, and fully IL verified. Once the IL verification is done, a series of RequestMinimum permissions stating things like access to various assemblies (referential verification), access to static method or variables, that some classes contain static fields, properties, or methods, etc... Since these are now permission requests, you can fine-tune exactly what the code can do to execute. Hell, if nothing else, I at least want the plug-in to make requests for application APIs so I can notify the user how the plug-in is going to interact with their environment.