通过 id_token 或 access_token IdentityServer4 Web 登录

IdentityServer4 Web Login via id_token or access_token

上下文

我正在构建一个混合 native/web 应用程序。其中一部分将由 API 驱动,一部分将是显示现有网站的网页视图。 使用我的 OIDC SDK (Amplify/AppAuth) 登录后,我可以访问用户的 id_tokenaccess_token.

但是,由于SDK中使用的webviews是不可控的,所以我不能重复使用我的身份服务器生成的cookies。

问题

出于上述原因,我正在尝试从他们的 id_token 或他们的 access_token 登录用户。 (都是 JWT)

我认为 IdentityServer4 不支持这一点,但我希望自己实现一些东西。

到目前为止我发现了什么:

1) id_token_hint

OpenID Connect Core 1.0, Section 3.1.2.1,id_token_hint参数:

OPTIONAL. ID Token previously issued by the Authorization Server being passed as a hint about the End-User's current or past authenticated session with the Client. If the End-User identified by the ID Token is logged in or is logged in by the request, then the Authorization Server returns a positive response; otherwise, it SHOULD return an error, such as login_required. When possible, an id_token_hint SHOULD be present when prompt=none is used and an invalid_request error MAY be returned if it is not; however, the server SHOULD respond successfully when possible, even if it is not present. The Authorization Server need not be listed as an audience of the ID Token when it is used as an id_token_hint value. If the ID Token received by the RP from the OP is encrypted, to use it as an id_token_hint, the Client MUST decrypt the signed ID Token contained within the encrypted ID Token. The Client MAY re-encrypt the signed ID token to the Authentication Server using a key that enables the server to decrypt the ID Token, and use the re-encrypted ID token as the id_token_hint value.

根据 OpenID 的这个可选规范,我应该能够使用 /authorize 端点上的 id_token 来登录用户。我知道这在 IdentityServer4 中没有实现,但我正在寻找 AddCustomAuthorizeRequestValidator 来做到这一点。但是我不确定如何在代码中“从他们的 id_token 中获取用户”。有什么指点吗?

2) 使用 AddJwtBearerClientAuthentication

这个方法听起来我可以从我的 access_token 进行身份验证,但我找不到太多关于如何使用它的文档。

问题

如果我误解了要求,请告诉我,但感觉就像您有 2 个不同的应用程序,并试图让它们感觉像是一个集成的屏幕集。

默认行为如下,因为 Web 视图是私人浏览器 sessions,不能使用系统浏览器 cookie。您想阻止第二步,因为它的用户体验很差:

  • 用户登录移动应用程序
  • 每当调用安全 Web 视图时,都会有一个新的私人浏览器 session,没有身份验证 cookie,因此需要新的登录名

常用方法:向 Web 视图提供令牌

如果这两个应用程序是相同用户体验的一部分,则将移动令牌转发到 Web 视图可能很常见。如果需要,扩展移动应用程序的范围以包括网络应用程序的范围。

您应该不需要颁发任何新令牌或更改任一应用程序的安全模型。但是移动和网络应用程序需要在解决方案上协同工作。

服务器端 Web 应用程序的可能 Web 视图解决方案

这可能是最简单的选择:

  • 提供需要令牌而不是 cookie 的新 .Net Core Web 后端入口​​点 URL
  • 然后移动应用程序可以从 Web 视图调用这些端点,并在 Web 视图请求的授权 Header 中提供自己的令牌
  • 网络后端然后可以将移动网络视图请求转发到您的网络 API

将授权 Header 添加到 Web 视图请求的代码可能有点棘手,但有一些方法可以做到:

SPA 的可能 Web 视图解决方案

适用于无 Cookie SPA 的选项是 Web 视图通过 Javascript 桥向移动主机应用程序请求访问令牌。我的一些代码为无 cookie SPA 执行此操作:

您可以从本地 PC 克隆/运行 移动 github 存储库以查看解决方案。我将在我的博客 Mobile Web Integration Post.

中写一些关于此的注释