IdentityServer4-API服务器如何与Identity Server通信

问题描述

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

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

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

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

此图向我们展示了:

enter image description here

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

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

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

我们应该可以:

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

我们还考虑了:

  • 将SP本身注册为IdP的特定客户和特定的索赔,
  • 此声明仅授予SP,并允许其发送请求以请求有关访问令牌的用户信息
  1. 此外,SP还可以在有限的时间内(等于访问的持续时间)存储与给定和先前发送给IdP(在AccessToken / User Infos词典中)的访问令牌有关的用户信息。访问令牌?这样可以防止服务提供商在访问令牌有效期间是否消耗了一定次数的资源,从而不断从访问令牌中询问用户信息

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

解决方法

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

SP稍后获得身份验证码时,会在后台单独发出请求,以获取真实的访问/ ID令牌。

一些进行此交换的简化代码如下:

/// <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令牌创建用户会话cookie,然后丢弃ID令牌。访问令牌也可以存储在此cookie中,或者您可以将其存储在内存中或其他地方。但这一切都取决于您的需求。

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...