Re: SecurityPermission problem



The Demand method skips the call stack frame for the method from which it is called. In order to have your assembly included in the stack walk initiated by Demand, you'll need to move it into a separate method since the Main method has no within-assembly callers. e.g.:

static void Main(string[] args)
{
DemandFileIOPermission();
FileStream fw = new FileStream("c:\\hello.txt", FileMode.Create);
}

private static void DemandFileIOPermission()
{
FileIOPermission fip = new FileIOPermission(FileIOPermissionAccess.AllAccess, "c:\\hello.txt");
fip.Demand();
}


"Itay Sandbank" <ItaySandbank@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:B50075F7-3042-45F6-901E-3B295B5D191A@xxxxxxxxxxxxxxxx
Hi.

I'm trying to understand how to use CAS, and found something strange. I'm
trying to deny my program of a few permissions to see what happens. I created
a small program that creates the file c:\hello.txt and exits:

[assembly: FileIOPermission(SecurityAction.RequestRefuse,
ViewAndModify="c:\\")]
namespace CodeAccessSecurity
{
class Program
{
static void Main(string[] args)
{
FileIOPermission fip = new
FileIOPermission(FileIOPermissionAccess.AllAccess, "c:\\hello.txt");
fip.Demand();
FileStream fw = new FileStream("c:\\hello.txt", FileMode.Create);
}
}
}

When I run it, I see a SecurityException thrown, as can be expected.
However, it is thrown when I create the FileStream and not when I Demand the
FileIOPermission.

When running from the local intranet zone (I changed the debugger's
security settings), the exception is thrown on the Demand - as I expected in
the first place.

What's going on here?

Thanks,
Itay.

.