2

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?

Greg Finzer
  • 6,714
  • 21
  • 80
  • 125

3 Answers3

1

This should do the trick:

Get current ASP.NET Trust Level programmatically

Community
  • 1
  • 1
zavaz
  • 745
  • 4
  • 13
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