Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么使用ConfigurationManager.GetSection会导致;SecurityException:请求失败";但是ConfigurationManager.OpenExeConfiguration没有?_C#_.net_.net 4.0 - Fatal编程技术网

C# 为什么使用ConfigurationManager.GetSection会导致;SecurityException:请求失败";但是ConfigurationManager.OpenExeConfiguration没有?

C# 为什么使用ConfigurationManager.GetSection会导致;SecurityException:请求失败";但是ConfigurationManager.OpenExeConfiguration没有?,c#,.net,.net-4.0,C#,.net,.net 4.0,我有一些奇怪的事情,希望.Net专家能帮我解决 我有一个自定义配置部分,为了获得它,我执行以下操作: var s = (TestConfigurationSection)ConfigurationManager .GetSection("testSection"); 我在我的开发机器上运行它(windows7,64位,Windows完全是最新的),它运行得很好 我将带有该代码的exe放入c:\users\public中的一个目录,在Windows Server 2008 R2计算机上,

我有一些奇怪的事情,希望.Net专家能帮我解决

我有一个自定义配置部分,为了获得它,我执行以下操作:

var s = (TestConfigurationSection)ConfigurationManager
    .GetSection("testSection");
我在我的开发机器上运行它(
windows7
,64位,Windows完全是最新的),它运行得很好

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

System.Configuration.ConfigurationErrorsException:为testSection创建配置节处理程序时出错:请求失败。(C:\Users\Public\configtest\AppConfigTestConsoleApplication.exe.Config第10行)--->System.Security.SecurityException:请求失败

现在,我更改了该代码以执行以下操作:

var config = ConfigurationManager.OpenExeConfiguration(
    ConfigurationUserLevel.None);
var s = (TestConfigurationSection) config
    .GetSection("testSection");
它在两台机器上都能正常工作

所以,我有点高兴(因为我的应用程序正在运行),但我头脑中的小精灵感到困惑,所以我在这里问:

为什么会这样?


复制步骤 在visual studio 2010中创建一个名为AppConfigTestConsoleApplication的新.net 4控制台应用程序项目,并将
程序.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>
然后添加应用程序配置文件并用以下内容替换内容:

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
C:\Users\Public\configtest>AppConfigTestConsoleApplication.exe
第一种方法失败了
System.Configuration.ConfigurationErrorsException:为testSection创建配置节处理程序时出错:请求失败。(C:\Users\Public\configtest\AppConfigTestConsoleApplication.exe.Config第10行)--->System.Security.SecurityException:请求失败。
在System.RuntimeMethodHandle.PerformSecurityCheck(对象obj、RuntimeMethodHandleInternal方法、RuntimeType父级、UInt32调用标志)
在System.RuntimeMethodHandle.PerformSecurityCheck(对象obj、IRuntimeMethodInfo方法、RuntimeType父级、UInt32调用标志)
在System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr、Binder Binder、Object[]参数、CultureInfo区域性)
位于System.Reflection.ConstructorInfo.Invoke(对象[]参数)
位于System.Configuration.TypeUtil.InvokeCortWithReflectionPermission(ConstructorInfo-ctor)
在System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord、FactoryRecord FactoryRecord、SectionRecord SectionRecord、Object parentConfig、ConfigXmlReader reader)
在System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions处(RuntimeConfigurationRecord configRecord、FactoryRecord FactoryRecord、SectionRecord SectionRecord、Object parentConfig、ConfigXmlReader reader)
在System.Configuration.RuntimeConfigurationRecord.CreateSection(布尔输入、FactoryRecord FactoryRecord、SectionRecord SectionRecord、Object parentConfig、ConfigXmlReader reader)
在System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted、FactoryRecord FactoryRecord、SectionRecord SectionRecord、Object parentConfig、ConfigXmlReader reader、字符串文件名、Int32行)
---内部异常堆栈跟踪的结束---
在System.Configuration.BaseConfigurationRecord.EvaluateOne(字符串[]键、SectionInput输入、布尔isTrusted、FactoryRecord FactoryRecord、SectionRecordsectionRecord、Object parentResult)
在System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord FactoryRecord、SectionRecord SectionRecord、Object parentResult、Boolean getLkg、Boolean getRuntimeObject、Object&result、Object&resultRuntimeObject)
位于System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串configKey、布尔getLkg、布尔checkPermission、布尔getRuntimeObject、布尔RequestISHER、对象与结果、对象与结果EntimeObject)
位于System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串configKey、布尔getLkg、布尔checkPermission、布尔getRuntimeObject、布尔RequestISHER、对象与结果、对象与结果EntimeObject)
位于System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串configKey、布尔getLkg、布尔checkPermission、布尔getRuntimeObject、布尔RequestISHER、对象与结果、对象与结果EntimeObject)
位于System.Configuration.BaseConfigurationRecord.GetSection(字符串configKey)
位于System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(字符串sectionName)
位于System.Configuration.ConfigurationManager.GetSection(字符串sectionName)
在AppConfigTestConsoleApplication.Program.Main()上
行动:“需求”
他问道:
'
拒绝设置:“”
授权集:“”
第二种方法有效:10
过程监视器 我运行并设置过滤器,如下所示:

剩下的508个事件都是:

  • 找不到名称
  • 没有更多的条目
  • 找不到路径
  • 仅用读卡器锁定文件
  • 没有这样的文件(对于C:\Windows\assembly\NativeImages\u v4.0.30319\u 32\mscorlib\93e7df09dacd5fef442c22d28efec83\mscorlib.ni.dllC:\Users\Public\configtest\AppConfigTestConsoleApplication.exe.config
  • 缓冲区溢出(用于HKCU\Control Panel\Desktop\MuiCached\MachinePreferredUILanguagesHKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Cache
有没有人对设置什么过滤器来找出根本原因有什么建议

:

检索通过合并应用程序配置文件(本地用户配置文件)获得的配置文件