This is by design (see the remarks section at
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemsecuritycodeaccesspermissionclassdemandtopic.asp for details).
Demands are intended to be made by code defining resources that require
protection. Their purpose is to determine whether code attempting to use a
resource possess the necessary permission(s), not whether the code defining
the resource has those same permissions. For example, the FileStream code
that actually accesses a file on disk makes a FileIOPermission demand. It
defines the resource, so it makes the demand. Since it's calling into
unmanaged code, it gets subjected to a different demand. However, there's
no point in asking it to fulfill the FileIOPermission demand that it invokes
since it can obviously bypass that same demand simply by not making it in
the first place.
[quoted text, click to view] "Itay Sandbank" <ItaySandbank@discussions.microsoft.com> wrote in message
news:2D68C4F7-E524-46A8-B62D-4BB2FCB0C0C0@microsoft.com...
> I get it, thanks.
>
> This is confusing behavior - having Demand check ALMOST everything. Is
> there a reason for it or is it a bug?
>
> Itay.
>
> "Nicole Calinoiu" wrote:
>
>> 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@discussions.microsoft.com> wrote in message
>> news:B50075F7-3042-45F6-901E-3B295B5D191A@microsoft.com...
>> > 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.
>>
>>