Problem
If you are signed in to an ASP.NET Core 2.0 application and then re-deploy (or just start/ stop) the application, the user is signed out and must then sign back in. This is very annoying for users.
Quick Solution
Based on this Stack Overflow post. The DirectoryInfo will create the folder at the root path of the server and the application name is the name of the website in the IIS Manager.
Note: You will need to have the server first running this code before signing in. After this code is running, the key will be persisted to the server and the subsequent deployments should not sign the user out until the lifetime expires. Additionally, be aware that the key persisted to the file is unencrypted.
Advanced Solution
There are other ways to deal with the keys being persisted. This may be important when your web servers cannot share the same directory on a single server. For more advanced situations, which require multiple servers and encryption, look at: .NET Core cookie authentication middleware, load balanci.ng and the data protection system.