2

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

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

4

1 回答 1

3

看起来有一个博客Find 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.
于 2012-03-20T03:11:00.097 回答