I am busy trying to understand security things in C # and I am struggling to understand how Assert works. I am using .net 3.5.
I made an example application to try to figure this out.
Call Method:
[FileIOPermission(SecurityAction.Deny, ViewAndModify = @"C:\")] static void Main(string[] args) { WriteTest testWriter = new WriteTest(); testWriter.Test(); Console.Read(); }
In a separate 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 does everything and everything. He will write a file. My question is how exactly does this work? Does this invalidate security classes if I can simply approve the permissions that I want them to pass checks? If I change Assert to Demand, it throws an exception.
Is the clause of security classes not allowing me to set permissions so that when I call a third-party class, I can prevent it from expelling and doing things that I do not want to do? I know that if I load a dll in AppDomain, I will get this effect, even if a third-party DLL uses Assert, it seems strange that if I call it directly, it will work. I tried reading the MSDN documentation on Assert, but it is hard for me to understand.
David esteves
source share