Identityserver4 无主题的Identity Server 4扩展授权

Identityserver4 无主题的Identity Server 4扩展授权,identityserver4,asp.net-core-3.1,identityserver4-extension-grants,Identityserver4,Asp.net Core 3.1,Identityserver4 Extension Grants,我创建了一个委托扩展授权,就像他们在文档中所做的那样 在本例中,它们从声明中获取用户身份,并返回授权验证结果,如下所示: var sub = result.Claims.FirstOrDefault(c => c.Type == "sub").Value; context.Result = new GrantValidationResult(sub, GrantType); 我的问题是,当我需要使用委托授权时,我并不总是有一个主题即用户身份。在我的场景中,我有一个应用程序在侦听消息。当

我创建了一个委托扩展授权,就像他们在文档中所做的那样

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

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

context.Result = new GrantValidationResult(sub, GrantType);
我的问题是,当我需要使用委托授权时,我并不总是有一个主题即用户身份。在我的场景中,我有一个应用程序在侦听消息。当应用程序收到消息时,它会使用客户端凭据调用API。然后,该API使用委托授权类型调用子API。由于应用程序正在使用客户端凭据,因此声明中没有子项

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


如何使用当前声明返回GrantValidationResult,而不返回不存在的主题?

我找到了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为null,则我执行以下操作:

context.Result = new GrantValidationResult(new Dictionary<string, object>());
这样做仍然会使用请求/验证的作用域填充声明