While I was upgrading Castle Windsor to support .NET 4.0 build scripts, I came across an interesting deprecated API. If you have set Warnings are Errors in your project settings, you’ll definitely get bitten by this once you upgrade your project from .NET 3.5 to .NET 4.0, if you’re using SecurityManager that is.

Let’s say you want to check for Unmanaged Code permission. In .NET 3.5 you’d do it like this:

1
return SecurityManager.IsGranted(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));

But this construct is deprecated in .NET 4.0. What you can do is to create a PermissionSet and compare it to the one on the current AppDomain. In case you need to support both versions of the .NET Framework, you still should use the old API:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private static bool CanAccessCredentials
{
get
{
#if NET35

return SecurityManager.IsGranted(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));

#else

var permission = new PermissionSet(PermissionState.None);
permission.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));

return permission.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet);

#endif
}
}