【问题标题】:Extension Grants - Invalid Grant Type Delegation - Identity Server 4 .NET Core 2.2扩展授权 - 无效的授权类型委派 - Identity Server 4 .NET Core 2.2
【发布时间】:2020-03-29 19:14:45
【问题描述】:

我正在尝试通过遵循HERE 中的教程来弄清楚如何实现delegation 授权类型和client credentials,这实际上是一页,因为我有API1 资源调用另一个API2 资源.

我已经实现了IExtensionGrantValidator,并使用他们提供的类名从文档中复制了代码,并添加了client,授权类型为delegation。但是,我不确定在哪里以及如何在下面调用此方法,起初我是从 client 调用它并尝试将我最初调用 API1 的 JWT 传递给 DelegateAsync 方法,但我一直得到一个 @987654329 @

在 API 1 中,您现在可以自己构建 HTTP 负载,或使用 IdentityModel 帮助程序库:

    public async Task<TokenResponse> DelegateAsync(string userToken)
    {
        var payload = new
        {
            token = userToken
        };

        // create token client
        var client = new TokenClient(disco.TokenEndpoint, "api1.client", "secret");

        // send custom grant to token endpoint, return response
        return await client.RequestCustomGrantAsync("delegation", "api2", payload);
    }

所以,我尝试从 API1 请求一个名为 GetAPI2Response 的方法中的令牌,该方法尝试调用 API2 中的方法:

        [HttpGet]
        [Route("getapi2response")]
        public async Task<string> GetApi2Response()
        {
            var client = new HttpClient();

            var tokenResponse = await client.RequestTokenAsync(new TokenRequest
            {
                Address = "http://localhost:5005/connect/token",
                GrantType = "delegation",
                ClientId = "api1_client",
                ClientSecret = "74c4148e-70f4-4fd9-b444-03002b177937",

                Parameters = { { "scope", "stateapi" } }
            });

            var apiClient = new HttpClient();
            apiClient.SetBearerToken(tokenResponse.AccessToken);

            var response = await apiClient.GetAsync("http://localhost:6050/api/values");

            if (!response.IsSuccessStatusCode)
            {
                Debug.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                return content;
            }
            return "failed";
        }

但是,在调试 invalid grant type 时会返回。奇怪的是,我注意到在运行 IDSRV 时,IExtensionGrantValidator 方法中的代码不会被命中,直到您单击 discovery docs 的链接,它才会显示为 grant type

我显然做错了,因为我没有包括前面提到的文档中的 DelegateAsync 方法,因为我不清楚它的去向。

【问题讨论】:

    标签: asp.net-core identityserver4


    【解决方案1】:

    文档似乎有点过时了。对于实际的扩展方法,必须有类似的东西:

    var tokenResponse = await client.RequestTokenAsync(new TokenRequest
    {
        Address = "http://localhost:5005/connect/token",
        GrantType = "delegation",
        ClientId = "api1_client",
        ClientSecret = "74c4148e-70f4-4fd9-b444-03002b177937",
        Parameters = new Dictionary<string, string>{{ "token", userToken }, { "scope", "stateapi" } }
    })
    

    您已经实现了它,但忘记添加初始令牌。当您从GetApi2Response() 中提取它时,它可以成为您的DelegateAsync

    那么您在 Identityserver 中的客户端配置必须包含 api1_clientdelegation GrantType。也不要忘记注册:

    services.AddIdentityServer().AddExtensionGrantValidator<YourIExtensionGrantValidatorImpl>()
    

    【讨论】:

    • 评论不用于扩展讨论;这个对话是moved to chat
    • 嗨,d_f,您从哪里获得 userToken?我无法想象如何获得它。
    • @JoseL.Garcia userToken 只是原始访问令牌,对“api1”的请求附带。它是请求的Authorization 标头中的第二个单词(第一个始终是bearer)。
    • 遗憾的是,我与@d_f 的聊天不再存在,但我记得从HttpContext 获得了原始访问令牌,用于调用我们移动聊天中的第二个api我>
    猜你喜欢
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    相关资源
    最近更新 更多