2013-02-21 6 views
8

In che modo un'applicazione .NET può rilevare il livello di attendibilità in cui è in esecuzione?Rileva livello di attendibilità

In particolare, sto volendo un modo per fare qualcosa di simile

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

mia soluzione attuale è quella di utilizzare

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

Ma, è un po 'di hack.

+0

[qui] (http://stackoverflow.com/a/11660205/969613) è qualcuno il controllo per vedere se sono in esecuzione come amministratore, potrebbe essere utile! – JMK

+1

@ JMK: i privilegi del sistema operativo e i privilegi CLR non sono collegati (il codice Silverlight verrà eseguito in media affidabilità per tutti gli utenti, ad esempio). –

+0

Ah ok, non lo sapevo, scusa! – JMK

risposta

-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

È possibile utilizzare questo codice:

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 è un sottoinsieme di .NET. –

+0

@Herman Schoenfeld - certo. Ma con le informazioni che ci hai fornito con la prima edizione della tua domanda, l'idea di questa risposta è stata quella di mostrarti la giusta direzione. – MikroDel

+0

Ok, una cosa del genere (senza i referee di asp.net) funzionerebbe .. –

6

Forse questo è utile:

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

O

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

Grazie Alex per la tua risposta. Meglio del tentativo/cattura che ho avuto. –

+0

@HermanSchoenfeld se questa risposta ti ha aiutato a risolvere il tuo problema, dovresti considerare [contrassegnandolo come risposta] (http://meta.stackexchange.com/a/5235/182513) – psubsee2003

+0

Per me, accedendo a 'ApplicationTrust' da un contesto parzialmente attendibile ha generato un'eccezione –

5

Da NET 4.0 in poi c'è la AppDomain.IsFullyTrusted property che potrebbe essere utile

Se si desidera testare questo sul dominio applicazione corrente, utilizzare:

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