// Determine if a specific permission has been granted.
public static bool IsGranted(IPermission perm)
{
// Bail out if the requested permission is null.
if(perm == null)
{
return true;
}
// Get the current permission state.
ClrPermissions current = ClrSecurity.GetPermissionsFrom(1);
if(current == null)
{
// Null is equivalent to "unrestricted".
return true;
}
// Build a permission set with just this permission.
PermissionSet set = new PermissionSet(PermissionState.None);
set.AddPermission(perm);
// If "PermitOnly" is set, then only check that set.
if(current.permitOnly != null)
{
return set.IsSubsetOf(current.permitOnly);
}
// The permission must be granted, but not denied.
if(!set.IsSubsetOf(current.granted) ||
set.IsSubsetOf(current.denied))
{
return false;
}
return true;
}