如何使我的应用程序在完全信任模式下运行 - .NET 4.0 控制台应用程序

发布于 2024-12-18 21:23:29 字数 7187 浏览 1 评论 0原文

我们在命令行(批处理)应用程序和完全/部分信任方面遇到问题。

我们之前的版本(版本号 7.13.0.63)工作正常,但是,当我们安装新版本(7.13.0.249)时,我们得到一条“该程序集不允许部分受信任的调用者”。

例外:

Stack Trace (edited):
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance[T]()
   at <Company>.Service.<Product>.Proxy.Factories.ControllerProxyFactory.Create[T]()
   <Stack Trace that leads to creation of a WCF client> ...

Inner Exception:
An error occurred creating the configuration section handler for system.serviceModel/behaviors: That assembly does not allow partially trusted callers. (C:\Program Files (x86)\InsuranceLine\ListLoader\InsuranceLine.ListLoader.Launcher.exe.Config line 53)
Stack Trace:
   at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecordsectionRecord, Object parentResult)
   at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.ServiceModel.Activation.AspNetEnvironment.UnsafeGetSectionFromConfigurationManager(String sectionPath)
   at System.ServiceModel.Activation.AspNetEnvironment.UnsafeGetConfigurationSection(String sectionPath)
   at System.ServiceModel.Configuration.ConfigurationHelpers.UnsafeGetAssociatedSection(ContextInformation evalContext, String sectionPath)
   at System.ServiceModel.Description.ConfigLoader.LookupChannel(ContextInformation configurationContext, String configurationName, ContractDescription contract, EndpointAddress address, Boolean wildcard, Boolean useChannelElementKind, ServiceEndpoint& serviceEndpoint)
   at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
   at System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress)
   <Stack Trace that determines the correct WCF factory to create>

Inner Exception:
That assembly does not allow partially trusted callers.
Stack Trace:
   at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(RuntimeAssembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandleInternal rmh, SecurityAction action, Object demand, IPermission permThatFailed)
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.ServiceModel.Configuration.ServiceModelExtensionCollectionElement`1.CreateNewSection(String name)
   at System.ServiceModel.Configuration.ServiceModelExtensionCollectionElement`1.DeserializeElementCore(XmlReader reader)
   at System.ServiceModel.Configuration.ServiceModelExtensionCollectionElement`1.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
   at System.Configuration.ConfigurationElementCollection.OnDeserializeUnrecognizedElement(String elementName, XmlReader reader)
   at System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
   at System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
   at System.Configuration.ConfigurationSection.DeserializeSection(XmlReader reader)
   at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
   at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
   at System.Configuration.RuntimeConfigurationRecord.CreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
   at System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader, String filename, Int32 line)

对于我们用于部署产品的流程,我们使用通过 WiX 创建的 MSI。我们执行以下步骤:

  • 卸载产品的先前版本
  • 从网络共享复制 msi(两个版本的共享相同,根据版本不同的子目录)
  • 以管理员身份安装 msi
    • 这将安装到 C:\Program Files (x86)

两个版本之间的唯一变化是:

  • 在 app.config 文件 (myapp.exe.config) 中包含额外的 AppSettings 项以指定事务
  • 超时“使用 new TransactionScope()” 到 “使用 New TransactionScope(TransactionScopeOption.Required, timeoutValueReadFromAppSettingsAndStoredInLocalVariable)”
    • WCF 客户端的创建不在此事务范围内。

附加信息:

  • 在我们的任何开发/暂存/质量保证/预生产环境中都不会发生异常。它只发生在我们的生产应用程序服务器上。
  • 我们正在向我们的组织推出 Windows 7,因此企业政策可能会发生变化。
  • 该应用程序面向 .Net Framework 4.0
  • 如果我们卸载新版本,然后重新安装旧版本(包括来自网络共享的副本),旧版本仍然可以正常工作
  • 安装到的计算机是 Windows Server 2008 R2
  • 该计算机是虚拟机 理想情况下

,我想知道一些事情:

  1. 如何更改程序集(或在安装后设置权限),以便它可以在完全信任模式下运行
  2. 如何在 dev/staging/ 中复制问题质量保证环境
    • 明确“不信任”程序集可能会重现它,但我想以与生产相同的方式重现它,以便在安装它时将其识别为不受信任,在我看来,执行此操作的方法会有一些东西为了“不信任”网络共享,我复制了 MSI,或者可能“不信任”MSI/程序集中的发布者信息。
    • 明确“不信任”
  3. 如何配置生产应用程序服务器/安全策略,以便将来的安装完全受信任(回答第 2 项很可能会回答这个问题)

谢谢

We are having an issue with a command line (batch) application and full/partial trust.

Our previous release (version number 7.13.0.63) works fine, however, when we install the new version (7.13.0.249) we get a “That assembly does not allow partially trusted callers.”

Exception:

Stack Trace (edited):
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance[T]()
   at <Company>.Service.<Product>.Proxy.Factories.ControllerProxyFactory.Create[T]()
   <Stack Trace that leads to creation of a WCF client> ...

Inner Exception:
An error occurred creating the configuration section handler for system.serviceModel/behaviors: That assembly does not allow partially trusted callers. (C:\Program Files (x86)\InsuranceLine\ListLoader\InsuranceLine.ListLoader.Launcher.exe.Config line 53)
Stack Trace:
   at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecordsectionRecord, Object parentResult)
   at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.ServiceModel.Activation.AspNetEnvironment.UnsafeGetSectionFromConfigurationManager(String sectionPath)
   at System.ServiceModel.Activation.AspNetEnvironment.UnsafeGetConfigurationSection(String sectionPath)
   at System.ServiceModel.Configuration.ConfigurationHelpers.UnsafeGetAssociatedSection(ContextInformation evalContext, String sectionPath)
   at System.ServiceModel.Description.ConfigLoader.LookupChannel(ContextInformation configurationContext, String configurationName, ContractDescription contract, EndpointAddress address, Boolean wildcard, Boolean useChannelElementKind, ServiceEndpoint& serviceEndpoint)
   at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
   at System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress)
   <Stack Trace that determines the correct WCF factory to create>

Inner Exception:
That assembly does not allow partially trusted callers.
Stack Trace:
   at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(RuntimeAssembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandleInternal rmh, SecurityAction action, Object demand, IPermission permThatFailed)
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.ServiceModel.Configuration.ServiceModelExtensionCollectionElement`1.CreateNewSection(String name)
   at System.ServiceModel.Configuration.ServiceModelExtensionCollectionElement`1.DeserializeElementCore(XmlReader reader)
   at System.ServiceModel.Configuration.ServiceModelExtensionCollectionElement`1.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
   at System.Configuration.ConfigurationElementCollection.OnDeserializeUnrecognizedElement(String elementName, XmlReader reader)
   at System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
   at System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
   at System.Configuration.ConfigurationSection.DeserializeSection(XmlReader reader)
   at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
   at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
   at System.Configuration.RuntimeConfigurationRecord.CreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
   at System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader, String filename, Int32 line)

In regards to the process that we use to deploy our product, we use a MSI created through WiX. We perform the following steps:

  • Uninstall the previous version of the product
  • Copy the msi from a network share (same share for both versions, different subdirectory based on versions)
  • Install the msi as an administrator
    • This installs to C:\Program Files (x86)

The only changes between the 2 versions are:

  • The inclusion of an extra AppSettings item in the app.config file (myapp.exe.config) to specify a transaction timeout
  • Changing a “using new TransactionScope()” to “using New TransactionScope(TransactionScopeOption.Required, timeoutValueReadFromAppSettingsAndStoredInLocalVariable)”
    • The creation of the WCF client is outside of this Transaction Scope.

Additional Information:

  • The exception does not occur in any of our dev/staging/qa/pre production environments. It only happens on our production application server.
  • We are in the process of rolling out Windows 7 to our organisation, so it is possible that enterprise policy has changed.
  • The application targets the .Net Framework 4.0
  • If we uninstall the new version, and reinstall the old version (including the copy from network share) the old version works fine still
  • The machine being installed to is Windows Server 2008 R2
  • The machine is a Virtual Machine that is hosted in a VMWare environment

Ideally I’d like to know a few things:

  1. How to alter the assembly (or set permissions after install) so that it can run in a full trust mode
  2. How to replicate the issue in dev/staging/QA environments
    • Explicitly "untrusting" the assembly might reproduce it, but I want to reproduce it in the same way as production so that when it is installed it is identified as untrusted, it seems to me that the way to do this would have something to do with “untrusting” the network share I copy the MSI from, or possibly “untrusting” the publisher information in the MSI/assembly.
  3. How to configure the production application server/security policy so that future installs are fully trusted (Answering item 2 will most likely answer this)

Thanks

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

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

发布评论

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

评论(1

怂人 2024-12-25 21:23:29

这确实很奇怪,因为 .NET 4.0 CLR 默认情况下不应用 CAS 安全策略,因此您实际上必须进行某种(希望如此)有意的更改才能使本地安装的命令行应用程序获得部分信任。

在尝试深入挖掘潜在原因之前,您能否验证以下内容:

  1. 您的 app.config 文件是否包含 NetFx40_LegacySecurityPolicy 元素?
  2. 您的应用程序实际上是在问题机器上的 4.0 CLR 下运行吗? (如果在抛出异常之前无法修改源以输出Environment.Version的值,Process Explorer 应该允许您确定进程中正在运行哪些 CLR 版本。)

This is really quite odd given that the .NET 4.0 CLR does not apply CAS security policy by default, so you would actually have to make a some sort of (hopefully) deliberate change to get a locally installed command line application to be partially trusted.

Before trying to dig too deep for potential causes, could you please verify the following:

  1. Does your app.config file contain a NetFx40_LegacySecurityPolicy element?
  2. Is your application actually running under the 4.0 CLR on the problem machine? (If you can't modify the source to output the value of Environment.Version before the exception is thrown, Process Explorer should allow you to determine which CLR version(s) are running in your process.)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文