How do I tell programatically if I am running under Medium Trust?
In C# how do I tell if my ass开发者_运维技巧embly is running under medium trust in asp.net? Is there something similar to Debugger.IsAttached for this?
This should do the trick:
Get current ASP.NET Trust Level programmatically
From dmitryr's blog post:
public static AspNetHostingPermissionLevel GetTrustLevel()
{
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;
}
Or you can use this slightly shorter version
public AspNetHostingPermissionLevel GetCurrentTrustLevel()
{
foreach (AspNetHostingPermissionLevel trustLevel in Enum.GetValues(typeof(AspNetHostingPermissionLevel)))
{
try
{
new AspNetHostingPermission(trustLevel).Demand();
}
catch (System.Security.SecurityException)
{
continue;
}
return trustLevel;
}
return AspNetHostingPermissionLevel.None;
}
精彩评论