sea*_*bun 8 c# asp.net asp.net-mvc antiforgerytoken
我的ASP.Net Core MVC应用程序添加了防伪中间件,如下所示:
startup.cs
services.AddMvc();
services.AddSession();
services.AddCaching();
services.AddSession(o =>
{
o.IdleTimeout = TimeSpan.FromMinutes(120);
});
services.AddAntiforgery();
Run Code Online (Sandbox Code Playgroud)
我在下面的视图和控制器中添加了
视图:
services.AddMvc();
services.AddSession();
services.AddCaching();
services.AddSession(o =>
{
o.IdleTimeout = TimeSpan.FromMinutes(120);
});
services.AddAntiforgery();
Run Code Online (Sandbox Code Playgroud)
控制者
[HttpPost, ValidateAntiForgeryToken]
public IActionResult Login(IFormCollection formCollection)
{...}
Run Code Online (Sandbox Code Playgroud)
问题是,当用户遇到不同的表单时,用户总是会跌倒。
System.InvalidOperationException:无法伪造令牌。---> System.Security.Cryptography.CryptographicException:在密钥环中找不到密钥{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}。
我找到了一个解决方案,建议在web.config中设置一对静态的验证/解密密钥,但是看来该解决方案仅适用于经典的asp.net应用程序。我应该如何在ASP.Net core中做?
Sco*_*ttC 13
我在Linux容器上托管的ASP .net核心应用程序中遇到了该错误。
从文档中看来,如果您不满足某些条件,则密钥仅会保留在该过程中-但这对我来说也不起作用。
首先,默认设置会发生错误。
然后,我为文件系统上的键添加了特定的配置:
services.AddDataProtection()
.PersistKeysToFileSystem(new System.IO.DirectoryInfo(@"/var/my-af-keys/"));
Run Code Online (Sandbox Code Playgroud)
这也没有解决我必须设置应用程序名称的问题:
services.AddDataProtection()
.SetApplicationName("fow-customer-portal")
.PersistKeysToFileSystem(new System.IO.DirectoryInfo(@"/var/dpkeys/"));
Run Code Online (Sandbox Code Playgroud)
我尚未确认,但LXC托管的可能性质意味着.net核心无法保留应用程序的身份。
Ste*_*ano 10
我有这个是因为我开始在本地主机端口上使用该应用程序,然后尝试在具有相同端口的 docker 中使用它。到那时我已经有了 cookie 指定了我的机器生成的密钥,所以它在 docker 上不起作用。
TL;DR如果您曾经使用过来自其他机器的域/端口,请清除您的 cookie。