In C# how do I tell if my assembly is running under medium trust in asp.net? Is there something similar to Debugger.IsAttached for this?
Asked
Active
Viewed 237 times
2
-
Possible duplicate: http://stackoverflow.com/questions/1064274/get-current-asp-net-trust-level-programmatically – Chris Shouts Jan 19 '11 at 13:55
3 Answers
1
This should do the trick:
0
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;
}

Martin Buberl
- 45,844
- 25
- 100
- 144
0
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;
}

Alex Mendez
- 5,120
- 1
- 25
- 23