2010-04-27 78 views
10

今天,我在嘗試遠程調試爲.NET 4.0運行時構建的應用程序時遇到了一個奇怪的問題。網絡共享上的.NET 4.0應用程序導致SecurityException

該應用程序駐留在網絡共享上並由遠程機器執行。但是,由於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> 
+0

你爲什麼要建立.NET 4.0,但迫使它運行舊版本的CLR的? – 2010-04-28 00:26:02

+0

對不起,我從我的測試代碼粘貼了錯誤的配置文件。我編輯了這個問題。然而,問題仍然存在。 – David 2010-04-28 07:44:10

回答

17

首先嚐試加載配置並打開部分上:

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

我跑進與.NET 4的同樣的問題,這對我的作品。

+0

非常感謝Timo,它也適用於我,但我認爲這是一個bug。 NET 4.0 – David 2010-05-24 07:39:04

+1

注意的是,雖然這工作異常周圍,有一個性能問題:。反序列化的XML格式不緩存,每次調用OpenExeConfiguration(時間)發生使用GetSection()「正確」(越野車)的方式將緩存節XML和會重用它subsequenct電話。這可能會或可能不會在你的應用程序會影響性能,取決於如何修復性能敏感的是,如何往往你做出OpenExeConfiguration電話。 – galaktor 2011-04-28 06:44:38

-1

我在這裏猜測,但我懷疑它是你的配置文件不被信任。

在你的情況,你的配置文件是引用類型ConsoleApplication1.AssetsSection不具有可作爲證據使用強名稱。

你能提供更多的細節和確切的錯誤信息。

+0

我有同樣的問題,我的應用程序的配置是這樣的,我仍然有問題<節名稱=「AuthenticationSection」 TYPE =「Cnbv.Sait.Configuration。AuthenticationConfigurationSection,Cnbv.Sait.Configuration,Version = 4.0.12.0,Culture = neutral,PublicKeyToken = 51d456dd8a5ff4be「requirePermission =」false「/> – 2010-10-14 23:23:24

1

如果您添加自己的映射類這樣的部分:在運行時

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

這是由於在.NET 4.0中一個已知的bug:

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

    public string MyAttr2 
    { 
     get; 
     set; 
    } 
} 

您可以使用此代碼來自網絡共享的應用程序。

以下代碼失敗並出現SecurityException。注意當您已經定義了部分自定義類型就像這個例子AssetsSection,它只是失敗:

ConfigurationManager.GetSection("test/assets"); 

一個補丁修復是解決方案建議由蒂莫使用不同的API。另一個解決方案是應用Microsoft提供的修補程序。

該bug和相關的修補程序KB2580188下提出。

+0

的偉大工程。 – 2014-01-15 10:10:07

相關問題