Where an ASP.NET Core application can write files, and how to keep Data Protection keys so users stay signed in Print

  • 0

Overview

This article answers two questions that usually come together:

  • Which folders can my application write to, and where do I put files that visitors must not be able to download?
  • Why are all my users signed out several times a day, and how do I stop it?

Where your application can write

On our ASP.NET hosting plans your application can create, change and delete files anywhere inside the web root of the site, which is the httpdocs folder, and in every folder below it. You do not need to set any permission for this.

For files that must never be served to a browser, use a folder inside httpdocs\App_Data, for example httpdocs\App_Data\keys or httpdocs\App_Data\uploads. The web server refuses every request for an address below /App_Data, so its contents cannot be downloaded, while your application reads and writes there normally. Create the folder in the Plesk File Manager or let the application create it.

Folders that you create beside httpdocs (at the same level, not inside it) are different: the application can read them but not write to them. Plesk does not let you change that yourself, and an attempt to do so ends with a message that the permissions cannot be changed. If you need such a folder to be writable, open a support ticket with the site name and the folder name and we will grant it. In most cases a folder in httpdocs\App_Data does the same job without waiting for us.

Why users are signed out: Data Protection keys

ASP.NET Core encrypts sign-in cookies, anti-forgery tokens and similar data with its Data Protection keys. On shared hosting the application has no user profile to keep these keys in, so unless you tell it where to store them it keeps them in memory only. Every time the application starts, it makes new keys, and everything encrypted with the old ones stops working.

The application starts again whenever its application pool restarts: after it has been idle for a while, after a recycle, and after each deployment. The results are:

  • Every user is signed out without warning.
  • Forms that were open show The antiforgery token could not be decrypted or return error 400.
  • The application log contains The key {…} was not found in the key ring or the warning Using an in-memory repository. Keys will not be persisted to storage.

Solution: store the keys in a folder

  1. Create the folder httpdocs\App_Data\keys for the site.
  2. In Program.cs, tell the application to keep its keys there:
    builder.Services.AddDataProtection()
        .PersistKeysToFileSystem(new DirectoryInfo(
            Path.Combine(builder.Environment.ContentRootPath, "App_Data", "keys")))
        .SetApplicationName("MyApplication");
    Use your own fixed name in place of MyApplication. This needs using Microsoft.AspNetCore.DataProtection; at the top of the file.
  3. Publish the application and sign in once. A file named key-….xml appears in the folder.
  4. From now on a restart of the application no longer signs anyone out.

Keep the folder when you publish

If the keys folder is deleted, everyone is signed out again. When you publish with Visual Studio or Web Deploy:

  • Either leave Remove additional files at destination switched off,
  • or switch on Exclude files from the App_Data folder in the publish settings, so the publish leaves App_Data alone.

When you publish by FTP or the File Manager, do not delete or overwrite the App_Data folder.

Good to know

  • The key files are as sensitive as a password. Keep them in App_Data or another folder that is not served, never in wwwroot.
  • Keys are renewed automatically about every 90 days. Old key files stay in the folder so that older cookies keep working. Do not delete them.
  • If the same site runs under more than one address or application, each needs its own folder or the same SetApplicationName value, depending on whether they should share sign-ins.
  • Persistent keys do not stop the application pool from restarting; they only make a restart invisible to signed-in users. To reduce restarts, see the article on preventing application recycles below.

Related articles


Was this answer helpful?

« Back