如何使用 Identity Server 4 授权码授权类型流程获取授权码

How to get Authorization Code using Identity Server 4 Authorization Code Grant Type Flow

我将 dotnet core (.NET Core) 3.1 与 IdentityServer4 包一起使用。我有一个 SPA,我希望与 Identity Server 4 集成,所以我正在沿着使用 PKCE 的 Authorization Code 授权类型的路径前进。我理解这种授权类型的基本概念,但是我不明白如何将 username/password 换成 授权码 。这是我的理解。

  1. SPA 向 /authorize 端点发出 GET 请求,看起来像 http://localhost:5000/connect/authorize?client_id=js&redirect_uri=http://localhost:5003/callback.html&response_type=code&scope=openid profile api1&state=8636d5233f77413584799be6cafe03a7&code_challenge=FbNm1tMkaRMzcSBv4_d5Rpq4VNaqyINVkCAcHsZkKV0&code_challenge_method=S256&response_mode=query
  2. 我认为这个 returns 到 http://localhost:5000/Account/Login 的 302 重定向应该是授权服务器托管的登录屏幕。
  3. 用户提交他们的凭据,auth 服务器验证凭据和 returns 另一个 302 重定向到给定的 redirect_url,在这种情况下 http://localhost:5003/callback.html。重定向 url 应包括生成的 授权码 和在查询字符串中提供的状态。
  4. SPA 验证状态,然后向 header 中包含的 /token 端点发出请求:authorization=Basic Y2xpZW50OnNlY3JldA== 并在查询字符串中:grant_type=authorization_code, code=授权码, redirect_uri=http://localhost/redirect
  5. 授权服务器returns一个有效的令牌。

我已经完成了第 1 步和第 2 步(还没有 UI,但这并不重要)。调用 /authorize 正在工作。我收到有效的 302 响应。我被卡住的地方是第 3 步和第 4 步。我是否需要编写一个端点来生成 授权代码 和 returns 包含它的 302 重定向或者这是什么东西已经内置到 Identity Server 4 中(如 /authorize 和 /token 端点)?

我已经混合了很多我在网上找到的例子,包括这个 https://identityserver4.readthedocs.io/en/latest/quickstarts/4_javascript_client.html。似乎没有使用这种类型的授权类型的任何完整示例。我还在我的创业公司中加入了一些测试资源、客户和用户。

services.AddIdentityServer()
    .AddDeveloperSigningCredential()
    .AddInMemoryApiResources(Config.GetAllApiResources())
    .AddInMemoryClients(Config.GetClients())
    .AddTestUsers(Config.GetUsers());

感谢您提供的任何帮助,至少可以将我推向正确的方向。

在验证凭据的帐户控制器中查看此 quickstart. In particular, the login action

有两个重要步骤:

  1. Sign in 到身份服务器(默认通过 cookie)
  2. Redirect to view that loads this javascript 以获得更好的 PKCE 体验。 url 包含由身份服务器生成和管理的查询参数 codestate

对于第 3 步,您确实需要 ui/endpoint 用于用户登录

IdentityServer4 团队通过创建一个您可以直接导入的模板让这一切变得简单 这是模板的 link - https://github.com/IdentityServer/IdentityServer4.Templates

在你的情况下,我认为你需要 is4ui 模板