【问题标题】:How to determine if the current application is Medium Trust如何判断当前应用是否为Medium Trust
【发布时间】:2011-09-13 14:21:53
【问题描述】:

我正在努力确保我的 ASP.Net 库能够在中等信任下工作。但是我遇到了问题,如果它在中等信任下运行,我需要禁用一些代码。

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

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

【问题讨论】:

    标签: c# asp.net medium-trust


    【解决方案1】:

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

    Finding out the current trust level in 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;
    }
    

    我刚刚在一个运行于 Medium 的 ASP.NET MVC3 应用程序中对其进行了测试,然后完全信任它,它会按照它所说的那样做。

    【讨论】:

    • 我想如果你只在Application_Start 时间做一次,那么在伟大的计划中这并不是一件大事。问题在于,您拥有的信任越少,您可以尝试的事情就越少,而这是您可以在所有信任级别下尝试的唯一一件事。
    猜你喜欢
    • 2011-03-09
    • 2021-08-09
    • 2013-05-26
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    相关资源
    最近更新 更多