【问题标题】:Get .NET to consider a specific network share to be fully trusted让 .NET 将特定网络共享视为完全受信任
【发布时间】:2010-11-12 10:19:44
【问题描述】:

我(仅)安装了 Visual Studio 2010。我正在尝试从命令行构建系统运行我的单元测试。我所有的开发工作都在我使用 Samba 共享的 Linux 服务器上的主目录中。当我尝试运行 NUnit 时,出现如下错误:

Unhandled Exception: System.TypeInitializationException: The type initializer for 'NUnit.ConsoleRunner.Runner' threw an exception. ---> System.Security.Security
Exception: That assembly does not allow partially trusted callers.
   at NUnit.ConsoleRunner.Runner..cctor()
The action that failed was:
LinkDemand
The assembly or AppDomain that failed was:
nunit-console-runner, Version=2.5.9.10305, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77
The method that caused the failure was:
NUnit.Core.Logger GetLogger(System.Type)
The Zone of the assembly that failed was:
Intranet
The Url of the assembly that failed was:
file:///Z:/gitrepos/smarties/dependencies/Windows-x86/NUnit-2.5.9.10305/bin/net-2.0/lib/nunit-console-runner.DLL
   --- End of inner exception stack trace ---
   at NUnit.ConsoleRunner.Runner.Main(String[] args)
   at NUnit.ConsoleRunner.Class1.Main(String[] args)

我已尝试使用 caspol.exe,如下所述:Edit and run .NET projects from network shares

>caspol -addgroup 1.2 -url file:///Z:/* FullTrust -name MyZShare
Microsoft (R) .NET Framework CasPol 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

WARNING: The .NET Framework does not apply CAS policy by default. Any settings shown or modified by CasPol will only affect applications that opt into using
CAS policy.

Please see http://go.microsoft.com/fwlink/?LinkId=131738 for more information.


The operation you are performing will alter security policy.
Are you sure you want to perform this operation? (yes/no)
yes
Added union code group with "-url" membership condition to the Machine level.
Success

效果不明显。

其他说明告诉我在控制面板的管理工具中找到一些 Microsoft .NET 安全策略工具,但我没有这样的工具可用。 (这是因为我只有 Visual Studio 2010,而不是更早的版本吗?我正在运行 Windows 7 Pro 64 位,如果有区别的话。)

我真的只是想让 .NET 相信这个目录在我自己的机器上,而不是 Intranet 的一部分。我不希望任何代码在部署时从网络共享运行,仅作为开发的一部分。我也不想盲目地信任所有网络共享,尽管即使这样也比每次我想运行测试时都将所有内容复制到本地驱动器上更容易接受。

在开发的这个阶段,我宁愿不必用强名称签署所有内容。 (而且我什至不确定它是否会有所帮助,因为 NUnit 显然在加载我的程序集之前就失败了。)


编辑 - 最后结果证明我试图解决错误的问题。在弄清楚如何让 NUnit 在没有子进程的情况下运行 .NET 4.0 测试时,我发现了这一点:

http://frater.wordpress.com/2010/05/04/debugging-nunit-tests-under-visual-studio-2010/

上面写着:

根据this page,您还应该在运行时元素下添加这一行 [in nunit-console.exe.config]:

<loadFromRemoteSources enabled="true" />

这允许从远程站点(例如网站)下载的库以完全信任的方式加载。我不确定为什么这是必要的,事实上,我的应用程序在没有它的情况下调试 NUnit 就好了。但是,如果您有问题,您可能想尝试一下。

这为我解决了我的问题,而无需与 caspol 搞混。

【问题讨论】:

  • 认为您真正想要做的是在 Internet 设置->安全中启用“本地 Intranet”区域,并将 UNC 路径添加到该区域。您也可以尝试调整信任设置。我对此不是 100% 确定,因此我将其作为评论而不是答案。

标签: .net security visual-studio-2010 nunit


【解决方案1】:

这是因为在 .NET 3.5 及更低版本中,位于网络共享上的所有库都被视为部分信任。可以通过使用更高版本的框架运行 NUnit 来解决此问题。

NUnit 运行器以 v3.5 框架为目标,可能是为了支持旧版。您可以使用 Visual Studio 附带的 corflags 工具检查这一点

> corflags nunit-console.exe
Version   : v2.0.50727

这意味着如果您从命令行运行 nunit-console.exe 并安装了 .NET 3.5 和 .NET 4,则 v3.5 运行时将用于运行可执行文件。您可以在控制台输出中看到这一点。

> nunit-console.exe
CLR Version: 2.0.50727.7905 ( Net 3.5 )

配置包含 <loadFromRemoteSources enabled="true"/> 元素,这会导致 .NET v4 忽略部分受信任的调用者错误,但在旧版本的框架中会忽略此设置。

要强制应用程序在 .NET 4 或更高版本下运行,请将以下行添加到应用程序配置文件的 startup 部分。

<supportedRuntime version="v4.0"/>

就我而言,在持续集成环境中运行测试之前,我必须在构建服务器自动化中添加一个步骤来更改此配置。

desc 'run unit tests'
task :tests => :compile do
  runner_dir = FileList["packages/NUnit.Runners*/tools"].first
  runner_config = File.join(runner_dir, "nunit-console.exe.config")

  # running the tests from a network share fails due to code access policy if the 
  # runner is on framework v3.5 or lower. force the runner to use v4.0 or above 
  comment = %<!-- Comment out the next line to force use of .NET 4.0 -->
  runtime_version = %<supportedRuntime version="v4.0.30319"/>
  File.write(runner_config, File.read(runner_config).sub(comment, runtime_version))

  nunit! :actually_run_tests do |nunit|
    nunit.command = File.join(runner_dir, "nunit-console.exe")
    nunit.assemblies = FileList["out/**/*.Tests.dll"]
    nunit.log_level = :verbose
  end
end

【讨论】:

  • 你真是个摇滚老兄。在看了这个问题几个小时后,你的修复为我做了!谢谢!!!
【解决方案2】:

我对 .net 4.0 中的安全性了解不多,无法为您提供解决方案,但我可以提供一些关于“缺失”的 Microsoft .net 安全策略工具的见解。

它已在 .net 4.0 中删除,因此任何引用它的示例都在 .net 4.0 之前。即使您安装 .net framework 2.0 以获得“SPT”,它也只会影响 .net 2.0、3.0 和 3.5 应用程序。

http://msdn.microsoft.com/en-us/library/2bc0cxhc.aspx

.Net 4.0 中的安全模型发生了广泛的变化,因此不应信任 4.0 之前的任何文档。

http://msdn.microsoft.com/en-us/library/dd233103.aspx

【讨论】:

    【解决方案3】:

    您确定 NUnit 是在 .NET 4.0 而不是早期的 .NET Framework 版本下运行吗? (即使您没有安装较早的 Visual Studio 版本,您也可能安装了较早的框架版本。)另外,您用来启动 NUnit 控制台应用程序的完整命令行(包括所有开关值)是什么?

    【讨论】:

    • 我相信 NUnit 在 .NET 2.0 下运行。确实有很多不同版本的 .NET 随 Visual Studio 2010 一起安装。但我似乎仍然缺少早期版本的配置工具。我运行此命令来调用 NUnit:dependencies\Windows-x86\NUnit-2.5.9.10305\bin\net-2.0\nunit-console-x86.exe -labels build\UpnpWidgetEmulator.Tests.dll 这是在 Z 上的目录上调用的:, 映射的网络驱动器。
    • 如果它在 2.0 下运行,您需要更改 2.0 CAS 策略来解决问题。如果您想要一个用于 2.0 x86 CAS 策略管理的 UI,您可以从 microsoft.com/downloads/en/… 下载 .NET 2.0 x86 SDK。
    • 我认为这个答案和评论确实是我要求的,所以我会将它们标记为已接受,但我实际上并没有尝试过 2.0 CAS 工具,因为我不需要它结束。我已经编辑了这个问题来解释我是如何解决我的问题的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多