Pab*_*iya 2 c# antiforgerytoken asp.net-core
步骤: 登录页面在两个不同的选项卡中打开。
(例外:Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException:提供的防伪令牌适用于与当前用户不同的基于声明的用户。)
任何解决方案来处理这个问题?
我同意@matt-shepherd 的观点,这是防伪令牌验证的正确行为。Tab B 处于过期状态,因为 Tab B 中的令牌没有反映我们已经在 Tab A 中登录,防伪令牌包含用户名。
我在这里发布另一个答案,因为在我的应用程序中(使用 asp.net 核心身份和剃刀页面的 .Net Core 2.2)System.Web.Helpers.AntiForgery.Validate()不可用。所以我无法按照@matt-shepherd 的建议验证控制器操作中的令牌.
相反,由于Patrick Westerhoff 的合并到 ASP.NET Core 2.2 代码库的请求,我创建了一个继承自 IAsyncAlwaysRunResultFilter 的过滤器:
public class RedirectAntiforgeryValidationFailedResultFilter : IAsyncAlwaysRunResultFilter
{
public Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
if (context.Result is AntiforgeryValidationFailedResult)
{
context.Result = new RedirectToPageResult("/AntiForgeryError");
}
return next();
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个名为AntiForgeryError.
最后,我已将我的应用程序配置为使用RedirectAntiforgeryValidationFailedResultFilterin Startup.cs:
services.AddMvc(options => options.Filters.Add<RedirectAntiforgeryValidationFailedResultFilter>())
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1024 次 |
| 最近记录: |