gpt4 book ai didi

c# - 为什么使用 ConfigurationManager.GetSection 会导致 "SecurityException: Request failed"但 ConfigurationManager.OpenExeConfiguration 不会?

转载 作者:可可西里 更新时间:2023-11-01 09:02:11 26 4
gpt4 key购买 nike

我有一些好奇,希望 .Net 专家可以帮助我。

我有一个自定义配置部分,为了掌握它,我这样做:

var s = (TestConfigurationSection)ConfigurationManager
.GetSection("testSection");

我在我的开发机器(Windows 7,64 位,Windows 完全最新)上运行它并且运行良好。

我将包含该代码的 exe 文件放入 Windows Server 2008 R2 机器上的 c:\users\public 目录中,打开一个以管理员身份运行命令提示符,运行它,我得到:

System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for testSection: Request failed. (C:\Users\Public\configtest\AppConfigTestConsoleApplication.exe.Config line 10) ---> System.Security.SecurityException: Request failed.

现在我更改了代码来执行此操作:

var config = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
var s = (TestConfigurationSection) config
.GetSection("testSection");

它在两台机器上都运行良好。

所以,我有点高兴(因为我的应用程序正在运行)但是我脑子里的那个小 Gremlin 很困惑所以我在这里问:

为什么会这样?


重现步骤

在 visual studio 2010 中创建一个名为 AppConfigTestConsoleApplication 的新 .net 4 控制台应用程序项目,并将 Program.cs 的内容替换为以下内容:

using System;
using System.Configuration;

namespace AppConfigTestConsoleApplication
{
public class TestConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("someSetting")]
public int SomeSetting
{
get { return (int) this["someSetting"]; }
set { this["someSetting"] = value; }
}
}

internal class Program
{
private static void Main()
{
try
{
var s = (TestConfigurationSection) ConfigurationManager
.GetSection("testSection");
Console.WriteLine("First Method worked: " + s.SomeSetting);
}
catch (Exception ex)
{
Console.WriteLine("First method failed");
Console.WriteLine(ex.ToString());

if (ex.InnerException != null)
{
var eex = ex.InnerException as SecurityException;
Console.WriteLine("Action: '{0}'", eex.Action.ToString());
Console.WriteLine("Demanded: '{0}'", eex.Demanded.ToString());
Console.WriteLine("RefusedSet: '{0}'", eex.RefusedSet);
Console.WriteLine("GrantedSet: '{0}'", eex.GrantedSet);
}

try
{
var config = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);

var s = (TestConfigurationSection) config
.GetSection("testSection");

Console.WriteLine("Second Method worked: "
+ s.SomeSetting);
}
catch (Exception x)
{
Console.WriteLine("Even the second method failed!");
Console.WriteLine(ex.ToString());
}
}
}
}
}

然后添加一个应用程序配置文件并将内容替换为以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section
name="testSection"
type="AppConfigTestConsoleApplication.TestConfigurationSection, AppConfigTestConsoleApplication"
requirePermission="false"
allowDefinition="Everywhere" />
</configSections>
<testSection someSetting="10"></testSection>
</configuration>

编译并运行,这是我得到的输出:

C:\Users\Public\configtest>AppConfigTestConsoleApplication.exe
First method failed
System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for testSection: Request failed. (C:\Users\Public\configtest\AppConfigTestConsoleApplication.exe.Config line 10) ---> System.Security.SecurityException: Request failed.
at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, RuntimeMethodHandleInternal method, RuntimeType parent, UInt32 invocationFlags)
at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, IRuntimeMethodInfo method, RuntimeType parent, UInt32 invocationFlags)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
at System.Configuration.TypeUtil.InvokeCtorWithReflectionPermission(ConstructorInfo ctor)
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)
--- End of inner exception 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 AppConfigTestConsoleApplication.Program.Main()
Action: 'Demand'
Demanded: '<PermissionSet class="System.Security.PermissionSet"
version="1"
Unrestricted="true"/>
'
RefusedSet: ''
GrantedSet: ''
Second Method worked: 10

进程监视器

我跑了 Process Monitor并像这样设置过滤器:

Process Monitor Filter

剩下的 508 个事件都是以下事件之一:

  • 找不到姓名
  • 没有更多条目
  • 找不到路径
  • 文件仅供读者使用
  • 没有这样的文件(对于 C:\Windows\assembly\NativeImages_v4.0.30319_32\mscorlib\93e7df09dacd5fef442cc22d28efec83\mscorlib.ni.dllC:\Users\Public\configtest 只有一次\AppConfigTestConsoleApplication.exe.config)
  • 缓冲区溢出(对于 HKCU\Control Panel\Desktop\MuiCached\MachinePreferredUILanguagesHKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Cache

有没有人对设置什么过滤器来找到根本原因有任何建议?

最佳答案

当程序集被“阻止”(在文件属性选项卡下)时,我有同样的行为。这些文件通过 zip 通过电子邮件发送给管理员。当他保存附件时,添加了 block 位……就像您从互联网上下载文件一样。在我们清除 block 后,它工作正常。

关于c# - 为什么使用 ConfigurationManager.GetSection 会导致 "SecurityException: Request failed"但 ConfigurationManager.OpenExeConfiguration 不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13528862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com