java mvc webfarm,MVC 6 WebFarm:无法解密防伪令牌

I'm running MVC 6 (vNext) in a webfarm scenario (ARR front-end with multiple AppServers). Server affinity is off.

When I bounce between app servers from one request to another I get the error

CryptographicException: The key {3275ccad-973d-43ca-930f-fbac4d276640} was not found in the key ring.

InvalidOperationException: The antiforgery token could not be decrypted.

Previously, I believe this was handled by setting a static MachineKey in the web.config.

As I understand it, we've now moved to a new DataProtection API and I've tried the following, thinking the application name is used as some kind of seed:

services.AddDataProtection();

services.ConfigureDataProtection(configure =>

{

configure.SetApplicationName("WebAppName");

});

which does not work to solve the problem.

Any idea how to solve this issue in vNext?

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值