In Asp.net Core Application, you can implementation the <machineKey>
element using the Data Protection API.
To use the Data Protection API in asp.net core application, first, install the package Microsoft.AspNetCore.DataProtection.SystemWeb
. You might meet a warning "Warning NU1701...This package may not be fully compatible with your project.", on my side, this warning will not influence us to use this package.
[Note] The new data protection system can only be installed into an existing ASP.NET application targeting .NET 4.5.1 or later. Installation will fail if the application targets .NET 4.5 or lower.
Then, register the Data Protection service in the Startup.cs file, like this:
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(@"D:\temp\temp-keys\"));
After running the application, the generate file content as below:
The above code was configured a file system-based key repository to store the machine key, you can also use Azure Storage, Redis, Registry or Entity Framework Core. More detail information, check the Key storage providers in ASP.NET Core