如何以编程方式告诉我是否在Medium Trust下运行?

时间:2011-01-19 13:50:56

标签: c# asp.net

在C#中如何判断我的程序集是否在asp.net中的中等信任下运行?是否有类似于Debugger.IsAttached的内容?

3 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

Get current ASP.NET Trust Level programmatically

答案 1 :(得分:0)

来自dmitryr的博客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;
}

答案 2 :(得分:0)

或者你可以使用这个稍短的版本

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