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

标签 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>

最佳答案

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

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

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

关于c# - 网络共享上的 .NET 4.0 应用程序导致 SecurityException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2725432/

相关文章:

c# - 多态方法不适用于 C# 4

c# - Expression.Block() 可以在 lambda 闭包中返回一个值吗?

c# - 发送多个值到 Mysql 查询

尝试应用里氏替换原理的 C# 编译错误

c# - ReaderWriterLockSlim 与双锁检查模式

c# - 如果 int 是 32 位。 ToString() 和 GetType() 等函数存储在哪里?

c# - .Net 程序集在将 bool 设置为 TRUE 时添加多余的 AND?

c# - 等待 IAsyncResult 函数完成

.net - 文件有多个属性时如何判断文件是否隐藏

c++ - mouse_event MOUSEEVENTF_LEFTDOWN 注册为选项卡而不是左键单击?