钥匙斗篷 Visual Studio

KeyCloak Visual Studio

我在使用 KeyCloak 登录时遇到错误。

我得到的错误是“访问令牌和刷新令牌都已过期

我已经按照此处详述的示例进行操作; https://github.com/dylanplecki/KeycloakOwinAuthentication/wiki/ASP.NET-MVC-Tutorial

该代码在登录页面之前有效。我输入我的登录 ID 和密码,按登录按钮,立即在黄色屏幕上出现上述错误。

我已经在主领域创建了客户端,并根据上面网页中的内容进行了所有设置。

有人知道我需要做什么来解决这个问题吗?

这已被确认为错误:

https://github.com/dylanplecki/KeycloakOwinAuthentication/issues/35#issuecomment-204128345

您还有问题吗?

解决此问题并正常使用 Keycloak 需要执行以下操作: 由于它被报告为错误,并且 The Library Author/maintainer 不再处理它,您必须修复您的本地副本,并使用它而不是 NuGet 包。

背景: Keycloak 在所有需要时间参考的属性中使用 UTC 时间,例如 Token 的 nbfexp。由于访问令牌通常在 5 分钟内有效,因此除非机器恰好位于与 UTC 时间匹配的时区,否则您将永远无法达到 5 分钟的限制。 该库的问题在于它将令牌中的时间与 DateTime.Now 进行比较,您需要手动将其更改为 DateTime.UtcNow

实施: 您可以从 GitHub here 下载库代码的副本,然后浏览解决方案中的第一个项目:KeycloakIdentityModel 到任务所在的文件:KeycloakIdentity.cs。 转到第 443 行,这是比较任务的开始 GetClaimsAsync.

任务里面有一个if语句,把里面的DateTime.Now都变成DateTime.UtcNow。结果应如下所示:

// Check to update cached claims, but not if refresh token is missing (as in bearer mode)
            if ((_kcClaims == null || _accessToken.ValidTo <= DateTime.UtcNow) && _refreshToken != null)
            {
                // Validate refresh token expiration
                if (_refreshToken.ValidTo <= DateTime.UtcNow)
                    throw new Exception("Both the access token and the refresh token have expired");

                // Load new identity from token endpoint via refresh token
                await RefreshIdentity(_refreshToken.RawData);
            }

更改发生在第 439 和 442 行。

重建解决方案后,浏览至解决方案的物理文件夹~\src\Owin.Security.Keycloak\bin\Debug,确保它们的修改日期与实际时间一致并复制这两个文件 KeycloakIdentityModel.dllOwin.Security.Keycloak.dll 到一个易于访问的目录(这不是必需的,但为简单起见建议这样做)。

然后在您的解决方案中,从引用中删除这两个引用并通过右键单击解决方案资源管理器中的引用节点并选择添加引用来添加新引用。浏览到您保存它们的位置,添加它们,这样它应该可以正常运行。