13

我开发了一个 .net 3.0 应用程序,它是使用 clickonce 部署的。

我想从完全信任转变为部分信任以简化部署。

我在visual studio下我的项目的“安全”选项卡中尝试了“计算权限”工具,答案很清楚:

---------------------------
Microsoft Visual Studio
---------------------------
This application requires full trust to run correctly.

但是,我无法弄清楚为什么需要完全信任。我尝试将安全设置更改为“部分信任”,但应用程序在启动时立即引发 SecurityException :

System.Security.SecurityException   {"Request failed.", Action= "System.Security.Permissions.SecurityAction.LinkDemand"
   at MyNameSpace.Program.Main(String[] args)
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
   at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
   at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
   at System.Activator.CreateInstance(ActivationContext activationContext)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

我的软件可能不需要完全信任(我只使用 https 连接到网络服务器,并且仅在用户请求时访问文件系统,用于导入/导出目的)

我怎样才能弄清楚为什么我的应用程序需要完全信任?

4

7 回答 7

10

看来我的问题是由于我的程序集是强签名的。

引用自msdn

在强名称程序集中,LinkDemand 应用于其中的所有可公开访问的方法、属性和事件,以将它们的使用限制为完全受信任的调用者。要禁用此功能,您必须应用 AllowPartiallyTrustedCallersAttribute 属性。

我正在向我的程序集添加所需的属性,我会让你知道结果如何:

[assembly:AllowPartiallyTrustedCallers]

更新:我已将属性添加到我的程序集中,但我也在使用一些 .net 程序集。

并非所有.net 程序集都可以被部分信任的程序集使用(这里有一个列表),即 WCF 程序集(即 System.ServiceModel)不在列表中

但是,Microsoft 声明可以在部分信任环境中使用 WCF(请参阅此处

我试图从我的引用中删除所有不需要的程序集,我在所有程序集中都使用了 AllowPartiallyTrustedCallers,但我仍然被卡住了......

于 2009-02-17T17:14:08.487 回答
4

Microsoft 有一个名为permcalc的工具,它分析程序集并生成详细的 xml 输出文件,如下所示:

<Type Name="MyClass">
<Method Sig="instance void .ctor()">
<Demand>
<PermissionSet version="1" class="System.Security.PermissionSet">
  <IPermission version="1" class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Unrestricted="true" /> 
  <IPermission version="1" class="System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Unrestricted="true" /> 
...
于 2009-02-17T19:32:16.720 回答
2

异常消息告诉您为什么不能以部分信任运行:

System.Security.Permissions.SecurityAction.LinkDemand

如果您将其复制并粘贴到 Google 中,您会在 MSDN 上找到几篇相关文章,这些文章可能会帮助您发现为什么您的应用程序需要完全信任。

于 2009-02-17T17:09:27.283 回答
2

在 app.config 的 configsections 中添加 requirePermission='false' 属性有很大帮助:

 <sectionGroup name="system.net" type="System.Net.Configuration.NetSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section requirePermission="false" name="defaultProxy" type="System.Net.Configuration.DefaultProxySection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </sectionGroup>

这对我来说是个窍门!

于 2009-02-18T09:37:24.957 回答
0

Hrm,只是一个猜测,但它是否正在运行网络共享?.NET 似乎根据运行代码的位置来分配信任。如果它来自除本地硬盘之外的任何地方,那么您将遇到安全问题。

于 2009-02-17T16:58:47.323 回答
0

如果没有看到您的应用程序的代码,就无法判断。您可能忽略了一些需要对您的应用程序完全信任的东西(可能是依赖关系?)。

于 2009-02-17T16:58:51.690 回答
0

您的堆栈跟踪未显示所要求的权限类型。

在这种情况下,AllowPartiallyTrustedCallers 不会帮助您。它应该在调用目标上指定,例如当一些部分受信任的代码调用到您的受信任程序集时。在您的情况下,您应该检查您的应用程序是否调用了未定义此属性的程序集。如果是,那么您的应用程序将需要在完全信任的情况下运行,并且根本无法在部分信任的情况下运行(这就是 CAS 的强制执行方式并且是设计使然。)

否则使用 permcalc。它将向您显示应在项目的安全设置中启用的权限。但是,我不确定在包含所有这些权限之后,您是否仍将拥有“部分信任”,或者更确切地说是完全信任并具有一些精简的权限。这是因为部分信任非常严格(打开 security.config 并查看启用的权限!),据我所知 WebPermission 不存在(发送 http 请求需要它),与 FileIOPermission 相同。

于 2009-02-17T20:14:03.733 回答