0

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

Marinos An
  • 9,481
  • 6
  • 63
  • 96
Tom Torell
  • 51
  • 5

2 Answers2

0

Try this:

public static bool IsFullyTrusted()
{
    try
    {
        new PermissionSet(PermissionState.Unrestricted).Demand();
        return true;
    }
    catch (SecurityException)
    {
        return false;
    }
}
György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
  • I can't see, if this is a solution for me: I think with this code, I can check, if my own code, actually running, is fully trusted. But I have to check, if the externalAssembly, I just loaded, is fully trusted. May be it was loaded via a network share, that is not fully trusted and only has lower intranet rights. – Tom Torell Jun 06 '16 at 15:37
0

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

Tom Torell
  • 51
  • 5