.NET 4.0 application on network share causes SecurityException

后端 未结 4 1823
深忆病人
深忆病人 2021-01-02 05:44

Today I experienced a weird problem while trying to remotely debug an application built for the .NET 4.0 runtime.

The application resides on a network share and exec

相关标签:
4条回答
  • 2021-01-02 06:00

    If you add your own class to map the section like this:

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

    You can use this code:

    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);
    
    0 讨论(0)
  • 2021-01-02 06:01

    I'm speculating here, but I suspect it's your configuration file that's not trusted.

    In your case, your configuration file is referencing a type ConsoleApplication1.AssetsSection that does not have a strong name that could be used as evidence.

    Can you provide more details and the exact error message.

    0 讨论(0)
  • 2021-01-02 06:03

    This is due to a known bug in .NET 4.0 when running the application from a network share.

    The follow code fails with a SecurityException. Note that it only fails when you have defined a custom type for the section like in this example AssetsSection:

    ConfigurationManager.GetSection("test/assets");
    

    One fix is the solution suggestion by Timo to use a different API. Another solution is to apply the patch provided by Microsoft.

    The bug and the related hotfix is filed under KB2580188.

    0 讨论(0)
  • 2021-01-02 06:12

    Try loading the configuration first and open your section on that:

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

    I ran into the same issue with .NET 4 and this works for me.

    0 讨论(0)
提交回复
热议问题