Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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# 网络共享上的.NET 4.0应用程序导致SecurityException_C#_.net_Clr_.net 4.0_Securityexception - Fatal编程技术网

C# 网络共享上的.NET 4.0应用程序导致SecurityException

C# 网络共享上的.NET 4.0应用程序导致SecurityException,c#,.net,clr,.net-4.0,securityexception,C#,.net,Clr,.net 4.0,Securityexception,今天,我在尝试远程调试为.NET4.0运行时构建的应用程序时遇到了一个奇怪的问题 应用程序驻留在网络共享上,由远程计算机执行。但是,由于System.Configuration.ConfigurationManager.GetSection()方法中的权限需求引发的SecurityException,应用程序每次在加载期间都会崩溃。我没有检查基类库中的其他权限要求是否也会导致安全异常,但在所有情况下,新CLR都不应该发生这种情况 应用程序在完全信任的情况下运行(在调试时进行了检查,对于CLR 4

今天,我在尝试远程调试为.NET4.0运行时构建的应用程序时遇到了一个奇怪的问题

应用程序驻留在网络共享上,由远程计算机执行。但是,由于System.Configuration.ConfigurationManager.GetSection()方法中的权限需求引发的SecurityException,应用程序每次在加载期间都会崩溃。我没有检查基类库中的其他权限要求是否也会导致安全异常,但在所有情况下,新CLR都不应该发生这种情况

应用程序在完全信任的情况下运行(在调试时进行了检查,对于CLR 4.0中的intranet应用程序,这一点通常必须是正确的),因此我不知道在这种情况下权限要求如何导致异常。基于3.5 SP1运行时构建时(默认情况下首先引入了网络共享应用程序的完全信任),everythings会按预期运行

我将示例代码粘贴到下面。非常感谢您的帮助

using System;
using System.Configuration;

namespace ConsoleApplication1
{
public sealed class AssetsSection : ConfigurationSection
{
    private static readonly ConfigurationProperty           s_propPath;
    private static readonly ConfigurationPropertyCollection s_properties;

    static AssetsSection()
    {
        s_propPath = new ConfigurationProperty("path", typeof(String));

        s_properties = new ConfigurationPropertyCollection()
        {
            s_propPath
        };
    }

    public static AssetsSection Get()
    {
        return (AssetsSection) ConfigurationManager.GetSection("test/assets");
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return s_properties;
        }
    }

    public String Path
    {
        get
        {
            return (String) base[s_propPath];
        }
        set
        {
            base[s_propPath] = value;
        }
    }
}

class Program
{
    static void Main(String[] args)
    {
        Console.WriteLine(AssetsSection.Get().Path);

        Console.ReadLine();
    }
}
}
和App.config文件

<?xml version="1.0"?>
<configuration>
<configSections>
    <sectionGroup name="test">
        <section name="assets" type="ConsoleApplication1.AssetsSection, ConsoleApplication1"/>
    </sectionGroup>
</configSections>

<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
</startup>

<test>
    <assets path="..\Assets"/>
</test>
</configuration>

我在这里猜测,但我怀疑是您的配置文件不可信

在您的情况下,配置文件引用的是一个类型
ConsoleApplication1.assetSection
,该类型没有可用作证据的强名称


您能否提供更多详细信息和确切的错误消息。

请尝试先加载配置,然后打开有关该配置的部分:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AssetsSection configSection = (AssetsSection)config.GetSection("test/assets");

我在.NET 4中遇到了同样的问题,这对我很有用。

如果您添加自己的类来映射该部分,如下所示:

[XmlRoot("Interface")]
public class MySectionClass
{
    [XmlAttribute()]
    public string MyAttr1
    {
        get;
        set;
    }

    public string MyAttr2
    {
        get;
        set;
    }
}
您可以使用以下代码:

ConfigurationSection configSection = 
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).
GetSection("MySection");

XmlSerializer xs = new XmlSerializer(typeof(MySectionClass));

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(configSection.SectionInformation.GetRawXml());

XmlNodeReader xnr = new XmlNodeReader(xdoc.DocumentElement);

MySectionClass section = (MySectionClass)xs.Deserialize(xnr);

这是由于从网络共享运行应用程序时.NET 4.0中存在已知错误造成的

以下代码失败,出现SecurityException。请注意,只有当您为该节定义了自定义类型(如本例中的
AssetsSection
)时,它才会失败:

ConfigurationManager.GetSection("test/assets");
一个解决方案是Timo建议使用不同的API。另一个解决方案是应用Microsoft提供的修补程序


该错误和相关修补程序已归档。

为什么为.NET 4.0构建,但强制它运行旧版本的CLR?很抱歉,我从测试代码中粘贴了错误的配置文件。我编辑了这个问题。当然,问题仍然存在。非常感谢蒂莫。这对我也很管用。但我假设这是.NET 4.0中的一个bug。请注意,虽然这可以解决异常问题,但存在一个性能问题:反序列化的xml不会被缓存,并且每次调用OpenExeConfiguration()时都会发生。使用GetSection()的“正确”(错误)方法缓存节XML,并在子序列调用中重用它。这可能会影响应用程序的性能,也可能不会影响,具体取决于它对性能的敏感程度以及您调用OpenExeConfiguration的频率。我也有同样的问题,我的应用程序配置看起来像这样,但我仍然有这个问题