IdentityServer3 上 UserService 中的客户端凭据

Client Credentials from within UserService on IdentityServer3

我需要连接到外部 API 以验证用户凭据并从我在 IdSrvr 中的自定义 UserService 中获取用户声明,但使用客户端凭据就像 IdentityServer 是连接到另一个服务的客户端一样。

方法应该是什么?

我首先想到的是在 UserService 中创建一个 HttpClient 实例以连接到 IdentityServer 本身并发出请求...但我不知道是否有 better/cleaner 方法。

有一项名为 ResourceOwner Grant 的授权。请相应地阅读spec

The credentials should only be used when there is a high degree of trust between the resource owner and the client (e.g., the client is part of the device operating system or a highly privileged
application)

大多数人强烈建议您不要将此授权用作反模​​式,它要求应用程序传递用户凭据,这违背了 OIDC 的整体理念。这笔赠款主要用于遗留用途。

OwinEnviroment 扩展方法让您可以发行令牌。

    public MyCustomUserService(OwinEnvironmentService owin)
    {
        _owin = owin;
    }

    public async Task AuthenticateLocalAsync(LocalAuthenticationContext context)
    {

        var token = await _owin.Environment.IssueClientToken(
            clientId: "Banana", 
            scope: "resource1", 
            lifetime: 3600);

       // call protected API with token
    }

Link to GitHub issue with same question