James Newton-King
James Newton-King

Reputation: 49042

Testing whether a .NET application is running with full trust

How do you test from .NET whether an application is running with full trust?

My application is running in .NET 2.0 and needs to work with that framework.

Upvotes: 2

Views: 262

Answers (1)

Erik Philips
Erik Philips

Reputation: 54628

Looks like there is a blog Finding out the current trust level in asp.net which looks promising. I pasted the code here in case the blog dies.

// Here is a sample code that returns the current trust level, assuming this
// code (or the calling code up the stack), is not loaded from GAC:

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

// The result can be cached in a static field 
// for the duration of the app domain lifetime.

Upvotes: 3

Related Questions