Using Code Access Security

bri189a

Senior Contributor
Joined
Sep 11, 2003
Messages
1,004
Location
VA
I'm trying to ensure that the calling assembly has reflection permissions prior to the user getting deep into the program and calling a method that requires it only to find they don't have it and losing they're work. I should be using code access security to do this, this is similiar to the example from MSDN on why to use CAS:
Requesting permissions informs the runtime which permissions your application needs to function or specifically does not want. For example, if your application writes to the local hard disk without using isolated storage, your application must have FileIOPermission. If your code does not request FileIOPermission and the local security settings do not allow your application to have this permission, a security exception is raised when the application attempts to write to the disk. Even if the application can handle the exception, it will not be allowed to write to the disk. This behavior might be frustrating to users if your application is a text-editing program that they have been using for an extended period of time.

The problem is that at the assembly level the only thing I seem to be able to check in the SecurityAttribute:
C#:
[[assembly: SecurityPermission(SecurityAction.RequestMinimum)]

But reading the rest of MSDN example your left with the impression that you can, and should, use any requests for permissions you need at the assembly level to raise errors when the assembly is loaded rather than down the line:
On the other hand, if your application requests FileIOPermission and the local security settings do not allow your application to have FileIOPermission, the application will generate the exception when it starts and the user will not face the problem of losing any work. Additionally, if your application requests FileIOPermission and if it is a trusted application, the administrator can adjust security policy to allow it to execute from the remote share.

So when I apply the below attribute:
C#:
[[assembly: ReflectionPermission(SecurityAction.Assert)]

I get the following compiler error:
Assembly generation failed -- SecurityAction type invalid on assembly.

So I said to myself, okay, maybe putting this attributes over the method, or the class level, when compiled into the assembly will somehow make these CAS policies checked when loading the assembly; I wrote a test app for that and unfortunately I could continue until I hit that code, which at that point I'm the frustrated user demonstrated in MSDN example.

So is there something wrong with my syntax? Am I misunderstanding something about CAS? I'm having trouble finding examples and people who are knowledgable about CAS - which if you think about is scarey; so I'm hoping someone here knows a bit.

Thanks.
 
Declarative code access permissions

The SecurityAction enumeration is used to specify whether the specified permission is essential (RequestMinimum), optional (RequestOptional), or should be denied (RequestRefuse). Only these three actions can be specified when applying declarative security attributes in this way.

It is the other properties of the attribute which specify exactly which permissions are being requested or denied.

I expect what you mean to use one of these:

Code:
[assembly: ReflectionPermission(SecurityAction.RequestMinimum, Unrestricted=true)]
[assembly: ReflectionPermission(SecurityAction.RequestMinimum, Flags=ReflectionPermissionFlag.AllFlags)]

etc.

Good luck :cool:
 
You should be able to use something like
C#:
[assembly:ReflectionPermission(SecurityAction.RequestMinimum, MemberAccess=true)]
I haven't got VS handy on this PC so I might be slightly off with the syntax though.
 
Last edited:
Re: Declarative code access permissions

Yeah, I happened across a CodeProject article that pointed out to me that only three of those 8 or 9 flags are applicable at the assembly level, and that's exactly what it was.

As usual it takes two people from across the pond to show a Yank how to use security properly :)

Thanks guys!
 
Back
Top