16

Is there an API to get the current ASP.NET Trust Level?

Luca Martinetti
  • 3,396
  • 6
  • 34
  • 49

2 Answers2

22

From dmitryr's blog:

AspNetHostingPermissionLevel GetCurrentTrustLevel() {
foreach (AspNetHostingPermissionLevel trustLevel in
        new AspNetHostingPermissionLevel [] {
            AspNetHostingPermissionLevel.Unrestricted,
            AspNetHostingPermissionLevel.High,
            AspNetHostingPermissionLevel.Medium,
            AspNetHostingPermissionLevel.Low,
            AspNetHostingPermissionLevel.Minimal 
        } ) {
    try {
        new AspNetHostingPermission(trustLevel).Demand();
    }
    catch (System.Security.SecurityException ) {
        continue;
    }

    return trustLevel;
 }

 return AspNetHostingPermissionLevel.None;
}
Aidiakapi
  • 6,034
  • 4
  • 33
  • 62
Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
0

The following code get current ASP.NET Trust Level programmatically using the official configuration API:

using System.Web;
using System.Web.Configuration;

...

var trust = WebConfigurationManager.GetSection("system.web/trust") as TrustSection;
return trust.Level;
Daniel Fisher lennybacon
  • 3,865
  • 1
  • 30
  • 38