以编程方式获取当前的ASP.NET信任级别

时间:2009-06-30 15:18:44

标签: asp.net

是否有API来获取当前的ASP.NET信任级别?

2 个答案:

答案 0 :(得分:22)

来自dmitryr's blog

AspNetHostingPermissionLevel GetCurrentTrustLevel() {
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;
}

答案 1 :(得分:0)

The following code get current ASP.NET Trust Level programmatically using the official configuration API:

using System.Web;
using System.Web.Configuration;

...

var trust = WebConfigurationManager.GetSection("system.web/trust") as TrustSection;
return trust.Level;