Encrypted config file does not work for Windows Service

让人想犯罪 __ 提交于 2020-02-24 13:26:11

问题


I am encrypting my app.config via the "aspnet_regiis -pef" approach.

Encryption and decryption work fine. If I decrypt the config file, the service runs fine. When I encrypt two sections of the config (via above method), the service refuses to start, w error below.

But at run time, the framework throws a ton of "exception while reading config..." errors (below).

I thought .config encryption was transparent to .NET apps generally? (under IIS, cmd line, etc.)

I can make the error come and go by decrypting: "aspnet_regiis -pdf" and then make it come back via "aspnet_regiis -pef"

The service is .NET 4.6, VS 2015, log4net and nhibernate.

To be clear, the .config is VALID. Here are the steps:

1) Clear, clean, unencrypted app.config: console app runs fine

2) paste in the < configProtectedData> section (required for encryption) , Then use "aspnet_regiis -pef" (which rewrites the config, if I damage it with the paste above, aspnet_regiis chokes -- if it works, config is valid )

3) Now the app won't run, error below.

4) OK, so I unencrypt via "aspnet_regiis -pdf", re add < configSections> and now the app works fine. (Again, if the xml were damaged, "aspnet_regiis -pdf" would blow up, but it does not, it works fine.)

I can repeat the above all day long.

One thing that I scratch my head over:

When you encrypt using "aspnet_regiis -pef" < configSections> is lost. aspnet_regiis.exe does not write it out. If you put it in manually after encryption, the System.Configuration module complains that there can only be one section beginning with the reserved "config" keyword.

I am not sure what the inner meaning of that is.

What direction should I go?

log4net:ERROR DefaultRepositorySelector: Exception while reading ConfigurationSe
ttings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed t
o initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognize
d configuration section hibernate-configuration. (C:\Inetpub\ProofConcept.Service.Se
rver\bin\Release\ProofConcept.Service.Server.exe.Config line 33)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignor
eLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(Configurat
ionSchemaErrors schemaErrors)
   at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey
)
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.PrivilegedConfigurationManager.GetSection(String sect
ionName)
   at System.Diagnostics.DiagnosticsConfiguration.Initialize()
   at System.Diagnostics.DiagnosticsConfiguration.get_IndentSize()
   at System.Diagnostics.TraceInternal.InitializeSettings()
   at System.Diagnostics.TraceInternal.WriteLine(String message)
   at log4net.Util.LogLog.EmitErrorLine(String message)
log4net:ERROR DefaultRepositorySelector: Exception while reading ConfigurationSe
ttings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed t
o initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognize
d configuration section hibernate-configuration. (C:\Inetpub\ProofConcept.Service.Se
rver\bin\Release\ProofConcept.Service.Server.exe.Config line 33)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignor
eLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(Configurat
ionSchemaErrors schemaErrors)
   at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey
)
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.get_AppSettings()
   at log4net.Util.SystemInfo.GetAppSetting(String key)
log4net:ERROR DefaultRepositorySelector: Exception while reading ConfigurationSe
ttings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed t
o initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognize
d configuration section hibernate-configuration. (C:\Inetpub\ProofConcept.Service.Se
rver\bin\Release\ProofConcept.Service.Server.exe.Config line 33)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignor
eLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(Configurat
ionSchemaErrors schemaErrors)
   at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey
)
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.get_AppSettings()
   at log4net.Util.SystemInfo.GetAppSetting(String key)
log4net:ERROR DefaultRepositorySelector: Exception while reading ConfigurationSe
ttings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed t
o initialize ---> System.Configuration.ConfigurationErrorsException: Unrecognize
d configuration section hibernate-configuration. (C:\Inetpub\ProofConcept.Service.Se
rver\bin\Release\ProofConcept.Service.Server.exe.Config line 33)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignor

来源:https://stackoverflow.com/questions/39338800/encrypted-config-file-does-not-work-for-windows-service

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!