Getting noddy CAS example working

C

codefragment

Hi
I'm stuck with the basics of CAS. I have a solution with two
projects, Assembly1 and Assembly2. Assembly1 is happily making calls
to the registry, filesystem, etc

Assembly1 calls a method on Assembly2, in the AssemblyInfo file for
Assembly2 I have these lines:

[assembly: AllowPartiallyTrustedCallers]
[assembly: FileIOPermission(SecurityAction.RequestMinimum, Read = @"D:
\temp\temp.txt")]
[assembly: FileIOPermission(SecurityAction.RequestOptional, Read = @"D:
\temp\temp.txt")]

and I have in a method

FileStream s = File.Open(@"D:\temp\temp.txt", FileMode.Open);

When I run this I get an error on that line which says
"Request for the permission of type
System.Security.Permissions.FileIOPermission (etc) failed]

If I remove the AssemblyInfo lines and just add this
[assembly: FileIOPermission(SecurityAction.RequestOptional, Read = @"D:
\temp\temp.txt", Unrestricted = true)]

then it works. I don't understand.
(1) Why do I need unrestricted access? I just want to read the file
(2) I though if you use RequestOptional it meant that you also needed
AllowPartiallyTrustedCallers for any calling assembly

please help, spent hours on this and I haven't a clue
 
C

codefragment

k, I assume its because I did:

FileStream s = File.Open(@"D:\temp\temp.txt", FileMode.Open);

and File.Open can do more than read, if I do this in the assembly:

[assembly: FileIOPermission(SecurityAction.RequestMinimum,All = @"D:
\temp\temp.txt")]
[assembly: FileIOPermission(SecurityAction.RequestOptional, All = @"D:
\temp\temp.txt")]

it works, replacing Read with All, or similiarly this (which to my
mind is clearer)

[assembly: FileIOPermission(SecurityAction.RequestMinimum,All = @"D:
\temp\temp.txt")]
[assembly: PermissionSet(SecurityAction.RequestOptional)]
 
A

Alberto Poblacion

I'm stuck with the basics of CAS. I have a solution with two
projects, Assembly1 and Assembly2. Assembly1 is happily making calls
to the registry, filesystem, etc

Assembly1 calls a method on Assembly2, in the AssemblyInfo file for
Assembly2 I have these lines:

[assembly: AllowPartiallyTrustedCallers]
[assembly: FileIOPermission(SecurityAction.RequestMinimum, Read = @"D:
\temp\temp.txt")]
[assembly: FileIOPermission(SecurityAction.RequestOptional, Read = @"D:
\temp\temp.txt")]

and I have in a method

FileStream s = File.Open(@"D:\temp\temp.txt", FileMode.Open);

When I run this I get an error on that line which says
"Request for the permission of type
System.Security.Permissions.FileIOPermission (etc) failed]

If I remove the AssemblyInfo lines and just add this
[assembly: FileIOPermission(SecurityAction.RequestOptional, Read = @"D:
\temp\temp.txt", Unrestricted = true)]

then it works. I don't understand.
(1) Why do I need unrestricted access? I just want to read the file
(2) I though if you use RequestOptional it meant that you also needed
AllowPartiallyTrustedCallers for any calling assembly

please help, spent hours on this and I haven't a clue

(1) The problem is in the line
FileStream s = File.Open(@"D:\temp\temp.txt", FileMode.Open);
This opens the file for reading AND WRITING, even if your code desn't
write anything to the Stream that you opened. So in the moment of executing
that statement you need read and write permission, which is granted by the
"unrestricted" permission request, but not by the "read" request.

(2) I understand that AllowPartiallyTrustedCallers is only needed in the
called assembly if it has a strong name and if it is being called from a
calling assembly that does not have Full Trust.
 
C

codefragment

Your reply came 2 minutes after my post :) But thanks, if I hadn't
found it your answer would have been enough for me to sort it

thanks
 
Top