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

本文关键字:信任 是否 应用程序 何确定 | 更新日期: 2023-09-27 17:59:03

我正在努力确保我的ASP。网络图书馆将在Medium Trust下运作。然而,我遇到了一些问题,如果在中等信任下运行,我需要禁用一些代码。

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

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

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

本文描述了一种确定信任级别的机制:

在ASP。净

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

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

我刚刚在ASP中测试了它。NET MVC3应用程序在Medium上运行,然后完全信任,它在锡上说什么就做什么。