测试.NET应用程序是否以完全信任的方式运行

时间:2012-03-20 02:13:05

标签: .net security full-trust

如何从.NET测试应用程序是否以完全信任的方式运行?

我的应用程序在.NET 2.0中运行,需要使用该框架。

1 个答案:

答案 0 :(得分:3)

看起来有一个看起来很有前途的博客Finding out the current trust level in asp.net。我在这里粘贴代码以防博客死亡。

// Here is a sample code that returns the current trust level, assuming this
// code (or the calling code up the stack), is not loaded from GAC:

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

// The result can be cached in a static field 
// for the duration of the app domain lifetime.