tags:

views:

44

answers:

2

Why do I need to Demand permission? Why can't it simply fail (commenting out the .Demand() call)?

ref: http://support.microsoft.com/kb/315529 Thanks!

try 
{
// Demand the permission to access the C:\Temp folder.
permFileIO.Demand();
resultText.Append("The demand for permission to access the C:\\Temp folder succeeded.\n\n");
}

-- edit 1 --

I read somewhere that CAS is going away because it is a pain to configure. Is this correct?

+1  A: 

You do not need the demand.

If you remove it and you have security restrictions it will simply throw a security exception.

The advantage of the demand is that the runtime can determine the exact rights needed by your application (e.g. that it needs FileIOPermission for a specific directory) and then let the user/admin configure that.

Foxfire
> the user/admin configure that.Please elaborate. Thanks.
rkrauter
This is a control panel applet that lets you exactly configure that. But as you wrote (correctly) CAS in this form is gone in .Net 4.0 because basically nobody ever did create rights-sets (there are billions (next to infinite) of possible rights-combinations) for each application.
Foxfire
+2  A: 

If you want to fail before you get to the call that needs permission, you can also decorate the calling method declaratively, which will fail before the method is even called. It also makes the code much cleaner, and easier to debug.

[FileIOPermission(SecurityAction.Demand, Write=@"C:\Temp ")]
public void MyTempFunction(){....
MrGumbe