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.