# OAuth2 认证失败：检测到 CSRF 错误

**URL:** <https://meta.discourse.org/t/oauth2-authentication-failure-csrf-detected-error/363932>\
**Category:** SSO\
**Tags:** oauth2\
**Created:** [2025年四月29日 08:02 UTC](https://meta.discourse.org/t/oauth2-authentication-failure-csrf-detected-error/363932 "2025-04-29T08:02:04Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Evie\_Tao](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/evie_tao/32/487964_2.png) [@Evie\_Tao](https://meta.discourse.org/u/Evie_Tao)\
**Post date:** [2025年四月29日 08:02 UTC](https://meta.discourse.org/t/oauth2-authentication-failure-csrf-detected-error/363932/1 "2025-04-29T08:02:04Z")

</div>

您好，  
我在使用 OAuth2 身份验证登录 Discourse 实例时遇到问题。用户尝试登录时会看到以下错误消息：

 ![图片显示了一个 Ruby on Rails 应用程序的堆栈跟踪，其中由于 OATH2 会话期间的 CSRF 检测导致身份验证失败。（由 AI 生成的标题）](https://global.discourse-cdn.com/meta/original/4X/b/2/4/b246155ecb2c42897b812cdd1cd9221fc02fe7f0.png)

这似乎与 OAuth2 回调阶段的 CSRF 令牌验证失败有关。

我已经检查了：

- 浏览器 Cookie 和会话
- 确保 OAuth2 设置和回调 URL 配置正确

但是，问题仍然存在，导致登录尝试持续失败。

谁能帮我弄清楚为什么会出现此错误以及如何解决它？

提前感谢您的帮助！

---

_[View the full topic](https://meta.discourse.org/t/oauth2-authentication-failure-csrf-detected-error/363932)._
