Server exceptions : "XmlException: Root element is missing" + "CryptographicException: An error occurred while trying to encrypt the provided data", After Azure portal updates.

Föreningen Lillåängar 1 Reputation point
2019-12-05T07:48:29.89+00:00

I have a web application hosted by an Azure App service. It has worked fine for years but suddenly I get an Exception when i try to enter the Account/login action: -->"XmlException: Root element is missing" + "CryptographicException: An error occurred while trying to encrypt the provided data".
If i got to Home/About action (which have [AllowAnonymous] attribute) that page works fine. But if i try to enter a page within the Account controller which have the [AllowAnonymous] attribute. That also throw the same Exception. So I am guessing the Exception occur in the constructor for the Account controller. See below.

I have not made any updates to the page in months and it has worked fine until now. If I run the application locally on my PC (connected to the same database on azure) it works fine. As I understand Azure have recently made updates to their portal. My guess is that the cause of the error is related to that.
Can you please help me solve this!

Constructor:
public AccountController(
        UserManager userManager,
        SignInManager signInManager,
        IEmailSender emailSender,
        ILogger logger,
        ApplicationDbContext context)
    {
        _userManager = userManager;
        _signInManager = signInManager;
        _emailSender = emailSender;
        _logger = logger;
        _context = context;
    }
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,180 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Luc Van der Veken 5 Reputation points
    2023-02-21T12:14:33.51+00:00

    This thread is 3 years old, but I wanted to share my experience anyway.

    I had

    • The same "Root element is missing" exception,
    • Also after working fine for months,
    • Once it started, rebooting didn't fix it.

    This was on some Raspberry Pi modules (6 out of about 100), all running the same Raspberry OS build with the same build of an AspNetCore app (ARM build, self-contained).

    What happened & how I solved it (in my case):

    AspNetCore stores data protection keys (1 file per key) either in a default location, or in the location you set after the services.AddDataProtection() call (your app may not have such a call, in that case the default is used).

    The default in my case -- Linux -- was ~/.aspnet/DataProtection-Keys/ .

    I found two xml files there, one of which was empty (size 0 bytes). Deleting the empty file and rebooting fixed the issue; a new file with another GUID in its name was automatically created at the next start.

    The problem will probably be the same on Azure or Windows, you'll just have to find where it stores those files.

    If you don't need the data protection, your app may not use it, but the runtime will use it anyway.

    As far as I can tell, you can prevent the problem from recurring by saying you don't need permanent keys (but read the warning in the docs, encrypted data will become undecipherable once the app exits):

    services.AddDataProtection().UseEphemeralDataProtectionProvider();

    Possible cause

    Judging by one discussion I found through Google, it could be that multithreaded apps that do not explicitly call AddDataProtection are susceptible to the problem, which would be the result of a race condition.

    Personally, in my case, I believe it's more likely to be caused by powering off without a clean shutdown.

    1 person found this answer helpful.
    0 comments No comments

  2. xequence 6 Reputation points
    2019-12-22T06:34:43.657+00:00

    Do you have parameters on your [AllowAnonymous] method? Typical account controller is setup to [Authorize] at the class level [Authorize] public class AccountController { ... }and each method that should allow anonymous (register, login) would [AllowAnonymous]. If it works on your box then there is something wrong with the deployment in relation to MSBuild. (check bindings in web.config)

    0 comments No comments