开发者

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;
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜