使.NET认为特定网络共享是完全可信的

7

我只安装了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:编辑并从网络共享运行 .NET 项目

>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相信这个目录在我的机器上而不是内部网络的一部分。我不希望任何代码在部署时从网络共享运行,只能作为开发的一部分运行。我也不想盲目地信任所有网络共享,尽管即使这样做也比每次运行测试时都将所有内容复制到本地驱动器上更容易解决。

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


编辑 - 最后,事实证明我试图解决错误的问题。在弄清楚如何在没有子进程的情况下运行.NET 4.0测试的同时,我找到了这个:

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

它说:

根据此页面,您还应在nunit-console.exe.config的运行时元素下添加此行:

<loadFromRemoteSources enabled="true" />

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

这为我解决了问题,而无需使用caspol。


我认为你实际想要做的是启用Internet设置->安全中的“本地 Intranet”区域,并将UNC路径添加到该区域。您还可以尝试调整信任设置。我不是100%确定,因此我将其作为评论而非答案。 - CodingGorilla
3个回答

5
这是因为在.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

你太棒了,伙计。经过几个小时的研究这个问题,你的解决方案为我解决了它!谢谢!!! - Denis

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


1

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


我相信NUnit正在.NET 2.0下运行。确实,Visual Studio 2010安装了许多不同版本的.NET。但是,我似乎仍然缺少早期版本的配置工具。我运行以下命令来调用NUnit:dependencies\Windows-x86\NUnit-2.5.9.10305\bin\net-2.0\nunit-console-x86.exe -labels build\UpnpWidgetEmulator.Tests.dll这是在Z:上映射的网络驱动器上的目录中调用的。 - Weeble
如果它在2.0下运行,您需要更改2.0 CAS策略以解决问题。如果您想要一个用于2.0 x86 CAS策略管理的UI,您可以从http://www.microsoft.com/downloads/en/details.aspx?FamilyID=fe6f2099-b7b4-4f47-a244-c96d69c35dec下载.NET 2.0 x86 SDK。 - Nicole Calinoiu
我认为这个回答和评论确实是我所要求的,所以我会标记它们为已接受,但实际上我并没有尝试过2.0 CAS工具,因为最终我并不需要它。我已经编辑了问题来解释我是如何解决我的问题的。 - Weeble

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接