An error occurred while reading the key ring but I don't even use DataProtection keys feature

44 Views Asked by At

I'm using ASP.NET Core 3.1 / C# in my program that is running on an embedded Linux system (Ubuntu), to access an HTTP web page (not secure).

The system image, which contains the OS and the program, is the same for all the embedded platforms that my company prepares, and everything works correctly - I can access the web page without any problems.

Recently, on some boards, the web page is displayed with errors. On these, when the program is launched, I get this error:

fail: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider[48]
An error occurred while reading the key ring.

System.Xml.XmlException: Root element is missing. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlReader.MoveToContent() at System.Xml.Linq.XElement.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XElement.Load(Stream stream, LoadOptions options)
at System.Xml.Linq.XElement.Load(Stream stream) at Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository.ReadElementFromFile(String fullPath) at Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository.GetAllElementsCore()+MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository.GetAllElements() at Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager.GetAllKeys() at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.CreateCacheableKeyRingCore(DateTimeOffset now, IKey keyJustAdded) at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.Microsoft.AspNetCore.DataProtection.KeyManagement.Internal.ICacheableKeyRingProvider.GetCacheableKeyRing(DateTimeOffset now) at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.GetCurrentKeyRingCore(DateTime utcNow, Boolean forceRefresh)

I don't use this "DataProtection keys" feature in my application. How to disable this feature?

1

There are 1 best solutions below

0
Aminos On BEST ANSWER

This link helped me to understand the problem https://learn.microsoft.com/en-us/answers/questions/2180/server-exceptions-xmlexception-root-element-is-mis

There was an empty data protection key inside ~/.aspnet/DataProtection-Keys/ deleting this last fixed the issue.

Using services.AddDataProtection().UseEphemeralDataProtectionProvider(); as described in the article does not avoid the exception if an invalid xml file exists.

Since I'm using ASP.Net core in an embedded system that can lose power and it's impossible to prevent ASP.NET Core from creating these files, I decided to delete all data protection keys files at application startup.