简体   繁体   English

Identityserver4登录ValidateAntiForgeryToken

[英]Identityserver4 Login ValidateAntiForgeryToken

I have identityserver4 working, but I am facing an issue I don't know how to resolve. 我有identityserver4工作,但我面临一个问题,我不知道如何解决。 I downloaded a sample project from their GitHub repo ( https://github.com/IdentityServer/IdentityServer4.Templates ). 我从他们的GitHub仓库( https://github.com/IdentityServer/IdentityServer4.Templates )下载了一个示例项目。 Using their IdentityServer4InMem sample, I am able to replicate exactly what I am experiencing. 使用他们的IdentityServer4InMem示例,我能够准确复制我正在经历的内容。

If I run the project and open two tabs with the following url: http://localhost:5000/Account/Login 如果我运行项目并使用以下URL打开两个选项卡: http:// localhost:5000 / Account / Login

If log in to tab 1, if I attempt to log in to tab 2 I get a HTTP ERROR 400. I've researched it has to do with the ValidateAntiForgeryToken, if I take off the annotation, I do not get the error. 如果登录到选项卡1,如果我尝试登录到选项卡2,我会收到HTTP ERROR 400.我研究过它与ValidateAntiForgeryToken有关,如果我取消注释,我就不会收到错误。 Of course I can not do that in production, so I am trying to find a solution. 当然我不能在生产中这样做,所以我试图找到一个解决方案。

This issue has nothing to do with Identity Server. 此问题与Identity Server无关。

To use [ValidateAntiForgeryToken] you need to @html.antiforgerytoken() in .cshtml file which creates a unique token in the hidden field but in .net core you don't need to use @html helper. 要使用[ValidateAntiForgeryToken],您需要在.cshtml文件中使用@ html.antiforgerytoken(),该文件在隐藏字段中创建唯一标记但在.net核心中您不需要使用@html帮助程序。 This hidden field's token gets generated when you make HttpGet request for the login page and then it passes when you click on login button. 当您为登录页面发出HttpGet请求时,会生成此隐藏字段的令牌,然后在您单击登录按钮时通过该令牌。

In your case when you are opening two tabs in browsers it creates token on a get request. 在您打开浏览器中的两个选项卡的情况下,它会在get请求中创建令牌。 And when you login from one tab (HttpPost request) your identity gets changed from anonymous user to some valid user in that case your previous token for an anonymos user is not valid anymore and it will always give you 400 status. 当您从一个选项卡(HttpPost请求)登录时,您的身份将从匿名用户更改为某个有效用户,在这种情况下,您以前的anonymos用户令牌不再有效,并且它将始终为您提供400状态。

This feature prevents cross-site request forgeries where it avoids any form submission after the user is authenticated. 此功能可防止跨站点请求伪造 ,避免在用户通过身份验证后提交任何表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM