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 に答える