2013-02-21 7 views
8

W jaki sposób aplikacja .NET może wykryć poziom zaufania, w którym działa?Wykryj poziom zaufania

Konkretnie, ja chcąc sposób, aby zrobić coś takiego

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

mojego obecnego rozwiązania jest wykorzystanie

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

Ale to trochę hack.

+0

[tutaj] (http://stackoverflow.com/a/11660205/969613) jest ktoś, sprawdzając, czy są one działający jako administrator może być przydatny! – JMK

+1

@JMK: Uprawnienia do systemu operacyjnego i CLR nie są połączone (kod silverlight będzie działać na średnim poziomie zaufania dla wszystkich użytkowników, na przykład). –

+0

Ah ok, nie wiedziałem tego, przepraszam! – JMK

Odpowiedz

-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

Można użyć tego kodu:

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 jest podzbiorem .NET. –

+0

@Herman Schoenfeld - na pewno. Ale dzięki informacjom, które nam dałeś z pierwszą edycją twojego pytania - ideą tej odpowiedzi było wskazanie właściwego kierunku. – MikroDel

+0

OK, coś w tym stylu (bez odwołań asp.net) zadziała .. –

6

Może to jest pomocne:

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

Albo

AppDomain.CurrentDomain.ApplicationTrust 
    .DefaultGrantSet.PermissionSet.IsUnrestricted(); 
+0

Dzięki Alex za odpowiedź. Lepsze niż try/catch, które miałem. –

+0

@HermanSchoenfeld, jeśli ta odpowiedź najlepiej pomogła Ci rozwiązać problem, powinieneś rozważyć [zaznaczenie go jako odpowiedź] (http://meta.stackexchange.com/a/5235/182513) – psubsee2003

+0

Dla mnie, dostęp do 'ApplicationTrust' od częściowo zaufany kontekst wyrzucił wyjątek –

5

Z .NET 4.0 począwszy tam jest AppDomain.IsFullyTrusted property który może być pomocne.

Jeśli chcesz przetestować na bieżącej aplikacji domeny, należy:

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