【问题标题】:IdentityServer4 - how does the API Server communicate with Identity ServerIdentityServer4 - API 服务器如何与 Identity Server 通信
【发布时间】:2020-08-22 16:32:54
【问题描述】:

我们目前正在使用 IdentityServer4 github 项目开发 API 和 IdentityServer:

Github 存储库:https://github.com/IdentityServer/IdentityServer4

文档:https://identityserver4.readthedocs.io/en/latest/

我们正在考虑 API 服务器或服务提供者 (SP) 与身份服务器或身份提供者 (IdP) 通信的最有效和安全的方式。我们还没有找到很多信息。

这张图向我们展示了:

服务提供商 (SP) 向身份提供商 (IdP) 请求用户信息(步骤 5:用户信息检索)

(来源:https://www.researchgate.net/figure/OpenID-Connect-Authorization-Code-Flow_fig1_320282638

  1. SP 如何向 IdP 询问这些信息?例如,他是否会发送包含用户发送的访问令牌的 HTTP Post “token-info”?
  2. 如果是这样,IdP 如何理解此请求来自 SP?

我们认为我们可以:

  • 在 IdP 和 SP 之间使用特定的 SSL 证书并以某种方式对其进行验证,
  • 还有 RSA 使用 IdP 已知的证书对访问令牌的哈希进行签名?

我们也想过:

  • 将 SP 本身注册为 IdP 的特定客户端和特定声明,
  • 此声明仅提供给 SP,并允许它发送请求以询问有关访问令牌的用户信息
  1. 此外,SP 有能力在有限的时间内存储用户关于给定和先前发送给 IdP 的访问令牌的信息(在 AccessToken / 用户信息的字典中),时间等于访问令牌?如果在访问令牌的有效期内资源被消耗了一定次数,这将阻止服务提供者不断地从访问令牌中询问用户信息

我们也可以提供代码,但我们认为这个问题主要是架构问题。

【问题讨论】:

  • 你的问题涵盖了很多。所以也许更好地提出更细粒度的问题,但我希望我的回答是一个起点:-)

标签: identityserver4 openid


【解决方案1】:

该图显示了用户如何进行身份验证并获取访问令牌。 SP 在 idP 中注册(使用 clientID/secret)。 SP 要求用户(浏览器)通过向浏览器发送重定向来进行身份验证。

SP 稍后在获取 auth-code 后,会在后台发出单独的请求以获取真正的 access/id-tokens。

执行此交换的一些简化代码如下所示:

/// <summary>
/// This method is called with the authorization code and state parameter
/// </summary>
/// <param name="code">authorization code generated by the authorization server. This code is relatively short-lived, typically lasting between 1 to 10 minutes depending on the OAuth service.</param>
/// <param name="state"></param>
/// <returns></returns>
[HttpPost]
public IActionResult Callback(string code, string state)
{

    //To be secure then the state parameter should be compared to the state sent in the previous step

    var url = new Url(_openIdSettings.token_endpoint);

    var token = url.PostUrlEncodedAsync(new
    {
        client_id = "authcodeflowclient",       //Id of this client
        client_secret = "mysecret",
        grant_type = "authorization_code",
        code_verifier = code_verifier,
        code = code,
        redirect_uri = "https://localhost:5001/CodeFlow/callback"

    }).ReceiveJson<Token>().Result;

    return View(token);
}

使用收到的令牌,由客户端决定将它们存储在哪里。在 ASP.NET 中,ID-token 用于创建用户会话 cookie,然后 ID-token 被丢弃。访问令牌也可以存储在此 cookie 中,或者您可以将其存储在内存或其他地方。但这一切都取决于您的需求。

一旦 SP 获得访问令牌,他就可以将该令牌传递给 API。但是,在 API 验证令牌签名之前,它首先向 IdP 询问其公共签名密钥(GET 请求),然后 API 使用它来验证令牌签名。

有关令牌签名和密钥的详细信息,请参阅page

【讨论】:

  • 非常感谢您的回答。我的问题是关于“获得真正访问/ id-tokens的单独请求”。我们假设这种请求是由 SP 隐含地向 IdP 发出的,但我们不太确定。如果是这样,SP 是否以某种方式将信息存储在内存中?再次感谢您的帮助,我们将继续学习!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 2020-05-13
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
相关资源
最近更新 更多