0

我按照他们在文档中所做的方式创建了一个委托扩展授权。( 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,但不存在不存在的主题?

4

1 回答 1

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>());

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

于 2020-03-09T15:27:24.167 回答