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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (11)

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

提问于
用户回答回答于

来自dmitryr的博客

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;
}
用户回答回答于

以下代码使用官方配置API以编程方式获取当前ASP.NET信任级别:

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

...

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

扫码关注云+社区