2013-02-21 83 views
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; 
     } 
    } 
} 

但是,這是一個黑客攻擊的一位。

+0

[這裏](http://stackoverflow.com/a/11660205/969613)是有人檢查,看看它們是否以管理員身份運行可能會很有用! – JMK 2013-02-21 12:04:51

+1

@JMK:操作系統權限和CLR權限未連接(例如,所有用戶的Silverlight代碼都將以中等信任運行)。 – 2013-02-21 12:09:25

+0

好的,我不知道,道歉! – JMK 2013-02-21 12:09:57

回答

-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 
      } 
     } 

    } 
} 
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; 
} 
+0

ASP.NET是.NET的一個子集。 – 2013-02-21 12:29:40

+0

@Herman Schoenfeld - 當然。但是,根據您提供的第一版問題的信息 - 這個答案的想法是向您展示正確的方向。 – MikroDel 2013-02-21 12:35:22

+0

好吧,類似的東西(沒有asp.net refereences)會工作.. – 2013-02-21 14:51:38

6

也許這是有幫助的:

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(); 
+0

感謝亞歷克斯的迴應。比我的try/catch更好。 – 2013-02-21 12:37:39

+0

@HermanSchoenfeld如果這個答案最能幫助你解決你的問題,你應該考慮[標記爲答案](http://meta.stackexchange.com/a/5235/182513) – psubsee2003 2013-02-22 10:14:49

+0

對我來說,訪問'ApplicationTrust'從部分信任的上下文引發異常 – 2013-09-13 07:43:33

5

從.NET 4.0開始還有就是AppDomain.IsFullyTrusted property這可能會有所幫助。

如果你想測試這個當前使用的應用領域,使用方法:

if (AppDomain.CurrentDomain.IsFullyTrusted) 
{ 
    // ... 
}