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

时间:2010-04-27 21:54:06

标签: c# .net clr .net-4.0 securityexception

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

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

应用程序以完全信任的方式运行(在调试时检查它,并且像往常一样,CLR 4.0中的Intranet应用程序必须始终如此)所以我很无能在这种情况下权限需求如何导致异常。当针对3.5 SP1运行时(默认情况下首次引入对网络共享应用程序的完全信任)构建时,每个运行都按预期运行。

我粘贴了下面的示例代码。非常感谢任何帮助。

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>

4 个答案:

答案 0 :(得分:17)

首先尝试加载配置并打开您的部分:

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

我遇到了与.NET 4相同的问题,这对我有用。

答案 1 :(得分:4)

这是因为从网络共享运行应用程序时,.NET 4.0中存在已知错误。

以下代码因SecurityException而失败。请注意,只有在为此部分定义了自定义类型时,它才会失败,例如AssetsSection

ConfigurationManager.GetSection("test/assets");

一个解决方案是Timo使用不同API的解决方案建议。另一种解决方案是应用Microsoft提供的补丁。

该错误及相关的修补程序归档于KB2580188

答案 2 :(得分:1)

如果你添加自己的类来映射这样的部分:

[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);

答案 3 :(得分:-1)

我在这里推测,但我怀疑这是你不配置的配置文件。

在您的情况下,您的配置文件引用的类型ConsoleApplication1.AssetsSection没有可用作证据的强名称。

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

相关问题