将 Assembly.LoadFrom(String, Evidence) 调用替换为 Assembly.LoadFrom(String) - .NET 4

发布于 2024-11-26 12:32:47 字数 508 浏览 1 评论 0 原文

我必须将一些旧的 .NET 2 代码移至 .NET 4 项目。它有下一行:

`return Assembly.LoadFrom(filePath, Assembly.GetExecutingAssembly().Evidence);`

正如 Microsoft 中所述 (MSDN):

注意:此 API 现已过时。未过时的替代方法是 LoadFrom(String)。

我不熟悉 Evidence 类。仅从调用中删除 Evidence 参数的风险是什么?当需要 Evidence 参数时,真实情况是什么?

预先感谢您的帮助

I have to move some old .NET 2 code to the .NET 4 project. It has the next line:

`return Assembly.LoadFrom(filePath, Assembly.GetExecutingAssembly().Evidence);`

As it said in Microsoft's (MSDN):

Note: This API is now obsolete. The non-obsolete alternative is LoadFrom(String).

I'm not familiar with the Evidence class. What is the risk of just removing the Evidence parameter from the call? What is the real case when the Evidence parameter is necessary?

Thanks in advance for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左耳近心 2024-12-03 12:32:47

证据参数用于提供代码访问安全(CAS)策略机制在决定向正在加载的程序集授予哪些 CAS 权限时可以使用的数据。但是,在 .NET 4.0 中,“裸”CLR 不再使用 CAS 策略 ( http://blogs.msdn.com/b/shawnfa/archive/2010/02/24/so-is-cas-dead-in-net-4-or-what.aspx, http://blogs.msdn.com/b/shawnfa/archive/2009/06/12/clr-v4-security-policy-roundup.aspx),所以使用了证据API 中已被弃用。通常只有两个原因导致人们可能在针对早期 .NET 版本的代码中的 LoadFrom 调用中提供证据:

  1. 允许程序集以比本来授予的权限更多的权限运行,或者
  2. 限制程序集的权限,删除根据程序集的默认证据授予的权限。

对于#1,您可能在 .NET 4.0 中无需执行任何操作,因为默认情况下所有代码都将以不受限制的 CAS 权限(也称为“完全信任”)运行。对于#2,推荐的方法是使用 沙盒应用程序域

The evidence argument was used to provide data that the Code Access Security (CAS) policy mechanism could use when deciding which CAS permissions to grant to the assembly being loaded. However, in .NET 4.0, the "naked" CLR no longer uses CAS policy ( http://blogs.msdn.com/b/shawnfa/archive/2010/02/24/so-is-cas-dead-in-net-4-or-what.aspx, http://blogs.msdn.com/b/shawnfa/archive/2009/06/12/clr-v4-security-policy-roundup.aspx), so the used of evidence in API has been deprecated. There are usually only two reasons why one might have provided evidence in a LoadFrom call in code targeting an earlier .NET version:

  1. To allow the assembly to run with more permissions than it might have otherwise been granted, or
  2. To restrict the permissions of the assembly, removing permissions that would otherwise have been granted under the default evidence for the assembly.

For #1, you probably have nothing to do in .NET 4.0 since all code will run with unrestricted CAS permissions (aka "full trust") by default. For #2, the recommended approach is to use a sandboxed appdomain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文