Tuesday, January 23, 2007 8:46 PM
DmitryR
Finding out the current trust level in ASP.NET
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.