how can I check the security setting of a loaded assembly at runtime with C# .NET 2.0 (VS 2005)? I'm loading the assembly with:
Assembly externalAssembly = Assembly.LoadFrom(path);
May be the path is local or it is a remote UNC Path (Network path).
If it is a remote network path, the user should set the CAS to "fulltrust" with caspol.exe, to run the application correctly. How can I check this at runtime, if CAS was configured right?
I've seen, .NET 4.0 provides a "IsFullyTrusted" property for this purpose.
Unfortunately I still have to use VS 2005 for my project.
Regards Tom
After I did some homework and dug into Code Access Security a little bit, I hope, I have found a solution for me so far. I need only two lines of code:
Assembly externalAssembly = Assembly.LoadFrom(path);
// Retrieve the permission set of the external assembly
PermissionSet permSet = SecurityManager.ResolvePolicy(externalAssembly.Evidence);
if(!permSet.IsUnrestricted())
{
throw new Exception("Assembly is not fully trusted!");
}
If the assembly has the unrestricted permission, IsUnrestricted() returns true, and the collection of permissions in permSet is empty.
If it is restricted, false is returned, and the permSet lists the permissions, that were assigned to the assembly by the .NET policy resolution.
Hope this helps someone in future
Tom