8

.NET 应用程序如何检测其运行所在的信任级别?

具体来说,我想要一种方法来做类似的事情

  if (RUNNING IN GREATER THAN MEDIUM TRUST) { 
    // set private fields & properties using reflection
  }

我目前的解决方案是使用

public static class CodeAccessSecurityTool {
    private static volatile bool _unrestrictedFeatureSet = false;
    private static volatile bool _determinedUnrestrictedFeatureSet = false;
    private static readonly object _threadLock = new object();

    public static bool HasUnrestrictedFeatureSet {
        get {
            if (!_determinedUnrestrictedFeatureSet)
                lock (_threadLock) {
                    if (!_determinedUnrestrictedFeatureSet) {
                        try {
                            // See if we're running in full trust
                            new PermissionSet(PermissionState.Unrestricted).Demand();
                            _unrestrictedFeatureSet = true;
                        } catch (SecurityException) {
                            _unrestrictedFeatureSet = false;
                        }
                        _determinedUnrestrictedFeatureSet = true;
                    }
                }
            return _unrestrictedFeatureSet;
        }
    }
}

但是,这有点骇人听闻。

4

4 回答 4

7

也许这会有所帮助:

ActivationContext ac = AppDomain.CurrentDomain.ActivationContext;
ApplicationIdentity ai = ac.Identity;
var applicationTrust = new System.Security.Policy.ApplicationTrust(ai);
var isUnrestricted = applicationTrust.DefaultGrantSet.PermissionSet.IsUnrestricted();

或者

AppDomain.CurrentDomain.ApplicationTrust
  .DefaultGrantSet.PermissionSet.IsUnrestricted();
于 2013-02-21T12:19:46.053 回答
6

从 .NET 4.0 开始,该AppDomain.IsFullyTrusted属性可能会有所帮助。

如果您希望在当前应用程序域上对此进行测试,请使用:

if (AppDomain.CurrentDomain.IsFullyTrusted)
{
    // ...
}
于 2015-12-27T10:21:15.843 回答
1

您可以使用以下代码:

private AspNetHostingPermissionLevel[] aspNetHostingPermissionLevel = new AspNetHostingPermissionLevel[] 
{  
   AspNetHostingPermissionLevel.Unrestricted, 
   AspNetHostingPermissionLevel.High, 
   AspNetHostingPermissionLevel.Medium,
   AspNetHostingPermissionLevel.Low,
   AspNetHostingPermissionLevel.Minimal
};

public AspNetHostingPermissionLevel GetTrustLevel()
{
   foreach (AspNetHostingPermissionLevel aspNetHostingPermissionLevel in aspNetHostingPermissionLevel)
   {
      try
      {
         new AspNetHostingPermission(aspNetHostingPermissionLevel).Demand();
      }
      catch (System.Security.SecurityException)
      {
         continue;
      }    

      return aspNetHostingPermissionLevel;
   }

   return AspNetHostingPermissionLevel.None;
}
于 2013-02-21T12:13:02.420 回答
-1
    public static class CodeAccessSecurity {
        private static volatile bool _unrestrictedFeatureSet = false;
        private static volatile bool _determinedUnrestrictedFeatureSet = false;
        private static readonly object _threadLock = new object();

        public static bool HasUnrestrictedFeatureSet {
            get {
#if __IOS__
                return false;
#elif __ANDROID__
                return false;
#else
                if (!_determinedUnrestrictedFeatureSet)
                    lock (_threadLock) {
                        if (!_determinedUnrestrictedFeatureSet) {
                            _unrestrictedFeatureSet = AppDomain.CurrentDomain.ApplicationTrust == null || AppDomain.CurrentDomain.ApplicationTrust.DefaultGrantSet.PermissionSet.IsUnrestricted();
                            _determinedUnrestrictedFeatureSet = true;
                        }
                    }
                return _unrestrictedFeatureSet;
#endif
            }
        }

    }
}
于 2013-10-16T06:12:17.710 回答