让.NET考虑特定的网络共享以完全信任

Wee*_*ble 7 .net security nunit visual-studio-2010

我(仅)安装了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)
Run Code Online (Sandbox Code Playgroud)

我尝试使用如下所述的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
Run Code Online (Sandbox Code Playgroud)

它没有明显的效果.

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

我真的只是希望.NET相信这个目录在我自己的机器上,而不是内部网的一部分.我不希望任何此代码在部署时从网络共享运行,仅作为开发的一部分.我也不想盲目地信任所有的网络共享,尽管每次我想运行测试时,除了将所有内容复制到本地驱动器之外,这将是一个更可口的解决方法.

在开发的这个阶段,我宁愿不必用强名来签名.(而且我甚至不确定它是否会有所帮助,因为NUnit在装载我的装配之前显然已经失败了.)


编辑 - 最后我发现我试图解决错误的问题.在找出如何让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捣乱.

Joe*_*lor 5

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

NUnit运行者以v3.5框架为目标,可能是为了传统支持.您可以使用Visual Studio附带的corflags工具进行检查

> corflags nunit-console.exe
Version   : v2.0.50727
Run Code Online (Sandbox Code Playgroud)

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

> nunit-console.exe
CLR Version: 2.0.50727.7905 ( Net 3.5 )
Run Code Online (Sandbox Code Playgroud)

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

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

<supportedRuntime version="v4.0"/>
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)