【问题标题】:Identity Server 4 Extension Grant without a Subject没有主题的 Identity Server 4 扩展授权
【发布时间】:2020-03-09 23:27:24
【问题描述】:

我按照他们在文档中所做的方式创建了一个委托扩展授权。 (https://identityserver4.readthedocs.io/en/latest/topics/extension_grants.html)

在示例中,他们从声明中获取用户的身份并返回授权验证结果,如下所示:

var sub = result.Claims.FirstOrDefault(c => c.Type == "sub").Value;

context.Result = new GrantValidationResult(sub, GrantType);

我的问题是,当我需要使用委托授权时,我并不总是拥有主题(即用户身份)。在我的场景中,我有一个监听消息的应用程序。当应用程序收到消息时,它会使用 client_credentials 调用 API。然后,该 API 使用委托授权类型调用子 API。由于该应用使用的是 client_credentials,因此声明中没有“子”。

我尝试检查“sub”声明是否存在,如果不存在,请将 GrantValidationResult 的主题设置为 IUserStore 的 FindByIdAsync 将查找的“神奇”guid,并返回 null 或新的空 TUser。在这两种情况下,这都会导致 Microsoft.AspNetCore.Identity 进一步轰炸管道。

我如何返回带有当前声明的 GrantValidationResult,但当它不存在时不返回主题?

【问题讨论】:

    标签: identityserver4 asp.net-core-3.1 identityserver4-extension-grants


    【解决方案1】:

    我为 GrantValidationResult 找到了这个覆盖。

    // Summary:
    //     Initializes a new instance of the IdentityServer4.Validation.GrantValidationResult
    //     class with no subject. Warning: the resulting access token will only contain
    //     the client identity.
    public GrantValidationResult(Dictionary<string, object> customResponse = null);
    

    由于我没有任何自定义响应,如果“sub”为空,那么我会这样做:

    context.Result = new GrantValidationResult(new Dictionary<string, object>());
    

    这样做仍然会使用请求/验证的范围填充声明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-17
      • 2019-05-06
      • 2020-01-11
      • 2018-08-06
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      相关资源
      最近更新 更多