I'm busy trying to understand the security stuff in c# and I'm struggling to see how Assert works. I'm using .net 3.5.
I made a sample app to try figure this out.
Calling method:
[FileIOPermission(SecurityAction.Deny, ViewAndModify = @"C:\")]
static void Main(string[] args)
{
WriteTest testWriter = new WriteTest();
testWriter.Test();
Console.Read();
}
In a seperate class library I have:
public class WriteTest
{
public void Test()
{
try
{
FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Write, @"C:\");
permission.Assert();
using (StreamWriter sw = new StreamWriter(@"C:\test.txt"))
{
sw.WriteLine("testing!");
sw.Flush();
}
Console.WriteLine("Writen to file!");
}
catch (SecurityException sec)
{
Console.WriteLine("No privileges!");
}
}
}
This code executes fine and all. It will write to the file. My question is how exactly does this work? Does this not invalidate the security classes if I can just Assert the permissions I want so that it skips the checks? If I change Assert to Demand it throws an exception.
Is the point of the security classes not to allow me to set permissions so that when I call a third party class I can prevent it from going rogue and doing stuff I don't want it to do? I know if I load the dll in an AppDomain I will get this effect even if the third party DLL does use Assert, it just seems strange that if I call it directly it will work. I've tried reading the MSDN documentation on Assert but I'm finding it hard to understand.