相关疑难解决方法(0)

为什么 AntiForgeryToken 验证总是失败?

我正在开发一个API使用asp.net core2和运行的网络应用程序Angular。详细的开发环境配置在这里。我正在尝试配置AntiForgeryToken验证,但它一直失败。我按照配置。在这里,但我不得不修改它,因为我的 angular 应用程序和 asp.net 服务器在两个不同的端口上运行,因为前端启动不生成令牌。我通过在允许我生成令牌的应用程序组件中调用API路径 ( /api/Account/ContactInitialization) 来启动后端ngOnInit。配置如下图,

IServiceCollection 服务:

        services.AddAntiforgery(options =>
                {
                    options.HeaderName = "X-CSRF-TOKEN";
                    options.SuppressXFrameOptionsHeader = false;
                });
Run Code Online (Sandbox Code Playgroud)

并在IApplicationBuilder Configure

app.Use(next => context =>
                {
                    string path = context.Request.Path.Value;
                    if (

                        string.Equals(path, "/", StringComparison.OrdinalIgnoreCase) ||
                        string.Equals(path, "/api/Account/ContactInitialization", StringComparison.OrdinalIgnoreCase) ||
                        string.Equals(path, "/index.html", StringComparison.OrdinalIgnoreCase))
                    {
                        // We can send the request token as a JavaScript-readable cookie, 
                        // and Angular will …
Run Code Online (Sandbox Code Playgroud)

c# antiforgerytoken csrf-protection asp.net-core-2.0

2
推荐指数
1
解决办法
7689
查看次数