2011-10-01 27 views
4

我有一個使用自帶WCF服務的.NET 3.5 Framework構建的Windows Forms應用程序。該服務&應用程序可以自行正常運行。使用SignedXml.ComputeSignature將簽名添加到配置文件後出現「無法識別的配置節」

有關具有地址&綁定信息在app.config文件中可訪問,我決定使用System.Security.Cryptography.Xml.SignedXml.ComputeSignature添加數字簽名。然後我將簽名添加到app.config並保存。這會在app.config中創建一個Signature元素作爲app.config文件配置節點的最後一個子節點。

我在開始服務之前添加了一個函數來檢查簽名。該應用程序正確地驗證簽名,但隨後嘗試啓動該服務時,它拋出以下嵌套錯誤:

  1. 爲「System.ServiceModel.DiagnosticUtility」的類型初始值引發異常。

2.配置系統初始化失敗

3.Unrecognized配置部分簽名。

這似乎並不重要,我把Signature元素放在app.config中。簽名總是被正確驗證,並且服務總是炸彈夾住無法識別的配置部分。註釋掉app.config中的Signature元素和代碼中的簽名檢查,該服務將重新開始,沒有問題。

爲什麼服務拋出這些錯誤,我能做些什麼來解決它們?

這裏是節錄的應用程序名稱&網址的app.config:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <section name="MyApp.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </sectionGroup> 
    </configSections> 
    <system.serviceModel> 
    <bindings> 
     <basicHttpBinding>  
     <binding name="MyAppServicePortBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <security mode="None"> 
      <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> 
      <message clientCredentialType="UserName" algorithmSuite="Default" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://myappurl/MyService" binding="basicHttpBinding" bindingConfiguration="MyAppServicePortBinding" contract="MyAppService" name="MyAppServicePort" /> 
    </client> 
    <services> 
     <service name="MyApp.MyService" behaviorConfiguration="MyAppServiceBehavior"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://mylocalservice:8080/LocalService" /> 
      </baseAddresses> 
     </host> 
     <!-- this endpoint is exposed at the base address provided by host --> 
     <endpoint address="" binding="wsHttpBinding" contract="MyApp.IServiceInit" bindingNamespace="http://mylocalservice:8080/LocalService" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="MyAppServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
    <applicationSettings> 
    <MyApp.My.MySettings> 
     <setting name="DefaultEntryType" serializeAs="String"> 
     <value>M</value> 
     </setting> 
     <setting name="CardTypes" serializeAs="String"> 
     <value>1111</value> 
     </setting> 
     <setting name="Freq" serializeAs="String"> 
     <value>120000</value> 
     </setting> 
    </MyApp.My.MySettings> 
    </applicationSettings> 
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> 
     <SignedInfo> 
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /> 
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /> 
      <Reference URI=""> 
       <Transforms> 
        <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /> 
       </Transforms> 
       <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /> 
       <DigestValue>jJYnz3j6LgxqdcUgvNSGNmJVum4=</DigestValue> 
      </Reference> 
     </SignedInfo> 
     <SignatureValue>czpn/uA31kMSoGFk2hi3SCYky6YM6/MjBT3lpMn7wluCjeFIFj0vJJZVI9ueQQn/RglFi8RIfAyov3rDwiS+pP/4b1Yh8KqNOftHMH9pC+CFsMHMQnIoPHyXVrFLpuU6rzjACdUky4zuB7I7Q5AHf1CF8F9PSEgIxiQ4gHgPhJCLujl6wvsMg3rXDHazRQ2Curj94iKUIsKo50X1dJxER1oWOB9g6QgzqsXTOmUkgGOygJrnrn1WQJ0UbWAvHHXIPZdD6jOL24vqhOYm55+b6hlkWdIvEvLBPVMtv2V8oQqxBpWRDh8ovMn4LQdgcFOpa/vG3ISXGp2oRzsCEpaxCQ==</SignatureValue> 
    </Signature> 
</configuration> 
+0

順便說一句,你的服務沒有拋出這些錯誤。 .NET配置系統拋出一個異常,因爲你的配置文件中有一個未知的部分。 –

+0

約翰 - 謝謝澄清。由於錯誤是在服務試圖啓動時拋出的,我只是將兩者聯繫起來,但對我來說服務本身是根本原因並不合適。 –

回答

5

你缺少一些必要的信息,以允許嵌入的app.config signutures。

http://www.beefycode.com/post/Managing-AppConfig-Integrity-using-Xml-Digital-Signatures.aspx約增加簽名的app.config文件:

我們不能只是撲通這個新元素在App.config並期望.NET配置管理器來處理它不知道它是什麼;這會在應用程序啓動期間導致失敗。這裏沒有特別的技巧,我們只需要通過將以下內容添加到配置文件的頂部來指示配置系統忽略此元素。

先輸入在App.config如下:

<configSections> 
    ... 
    <section name="Signature" type="System.Configuration.IgnoreSectionHandler" /> 
</configSections> 

鑑於上述鏈接獲取完整的app.config和使用示例。它應該完成這項工作。

+0

謝謝 - 這正是我需要的。以爲有可能我可以添加到configSections,但不知道要搜索什麼。剛剛測試了這個變化,它像冠軍一樣工作。你讓我的週末! :) –

+0

嗯,非常重要。我想在配置文件中存儲額外的自定義數據,並且這個配方可以讓它通過配置系統解析。 – hypersw

相關問題