如何确定当前应用程序是否为中等信任

Ear*_*rlz 5 c# asp.net medium-trust

我正在努力确保我的ASP.Net库在Medium Trust下运行.我遇到了问题,因为如果它在中等信任下运行,我需要禁用一些代码.

如何从C#确定当前应用程序是否为中等信任?

具体来说,我正在尝试从web.config中读取customErrors部分,并且我遇到了安全性错误

Kev*_*Kev 3

本文介绍了一种确定信任级别的机制:

找出 ASP.NET 中当前的信任级别

这是代码,以防链接失效:

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;
}
Run Code Online (Sandbox Code Playgroud)

我刚刚在一个 ASP.NET MVC3 应用程序中测试了它,该应用程序运行在 Medium 上,然后运行在 Full tr​​ust 上,并且它按照它的说明进行操作。