MVC 6 WebFarm: The antiforgery token could not be decrypted

我是研究僧i 提交于 2019-12-03 14:22:49

Explanation

You'll need to reuse the same key.

If you are on Azure, the keys are synced by NAS-type storage on %HOME%\ASP.NET\DataProtection-Keys.

For locally run application, they are stored in the %LOCALAPPDATA%\ASP.NET\DataProtection-Keys of the user running the application or stored in the registry if it's being executed in IIS.

If none of the above match, the key is generated for the lifetime of the process.

Solution

So the first option is not available (Azure only). However, you could sync the keys from %LOCALAPPDATA%\ASP.NET\DataProtection-Keys of the user running your application on each machine running your application.

But even better, you could just point it to a network share like this:

sc.ConfigureDataProtection(configure =>
{
    // persist keys to a specific directory
    configure.PersistKeysToFileSystem(new DirectoryInfo(@"Z:\temp-keys\"));
});

This will allow you to scale while keeping your security.

Important: Your keys will expire every 90 days. It will be important to regenerate them frequently.

You can change it using this bit of code but the shorter, the safer you are.

services.ConfigureDataProtection(configure =>
{
    // use 14-day lifetime instead of 90-day lifetime
    configure.SetDefaultKeyLifetime(TimeSpan.FromDays(14));
});

Source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!