【问题标题】:IdentityServer4 - RequestClientCredentialsTokenAsync returning unauthorized_clientIdentityServer4 - RequestClientCredentialsTokenAsync 返回未授权客户端
【发布时间】:2019-12-19 20:15:10
【问题描述】:

使用 IdentityServer4,我在为 .net core 3 更新代码后收到了未授权客户端错误。

我的客户端在IdentityServer中设置如下:

                new Client
                {
                    ClientId = "testclient",
                    ClientName = "My Test Client",
                    RequireConsent = false,


                    AllowedGrantTypes = GrantTypes.Implicit,

                    ClientSecrets = { new Secret("secret".Sha256()) },

                    RedirectUris = { "https://localhost:50691/signin-oidc" },
                    PostLogoutRedirectUris = { "https://localhost:50691/signout-callback-oidc" },

                    AllowedScopes = new List<string>
                    {
                        IdentityServerConstants.StandardScopes.OpenId, // identity resource
                        "testscope" // api resource
                    }
                },

我正在尝试在我的客户端应用程序中使用以下代码检索访问令牌:


            public async Task<TokenResponse> GetAccessTokenAsync(string IdentityServerBaseAddress, string IdentityServerClientId)
            {
                var client = new HttpClient();

                var disco = await client.GetDiscoveryDocumentAsync(IdentityServerBaseAddress);

                if (disco.IsError)
                {
                    Console.WriteLine($"Disco error: {disco.Error}");
                    return null;
                }

                Console.WriteLine($"Token endpoint: {disco.TokenEndpoint}");
                Console.WriteLine();

                // TODO: Get secret from Azure Key Vault
                // request token
                var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
                {
                    Address = disco.TokenEndpoint, // "https://localhost:5000/connect/token"

                    ClientId = "testclient", // valid clientid
                    ClientSecret = "secret",
                    Scope = "testscope"
                });

                if (tokenResponse.IsError)
                {
                    Console.WriteLine(tokenResponse.Error);
                    return null;
                }

                return tokenResponse;
            }

tokenResponse.IsError 返回 true(因此该方法返回 null)并且 Error 设置为“unauthorized_client”。我将此代码基于http://docs.identityserver.io/en/latest/quickstarts/1_client_credentials.html?highlight=requestclientcredentialstokenasync 的文档。我的 clientid 是有效的,并且 IdentityServer 正在为该客户端登录时验证用户。我很确定这个问题很简单,我只是没有看到?任何帮助将不胜感激。

【问题讨论】:

    标签: asp.net-core identityserver4


    【解决方案1】:

    由于您使用的是 clientId 和 ClientSecret,所以我认为您需要更改您的客户端配置以使用 GrantTypes.ClientCredentials,而不是 GrantTypes.Implicit。

    变化:

    AllowedGrantTypes = GrantTypes.Implicit,
    

    AllowedGrantTypes = GrantTypes.ClientCredentials,
    

    【讨论】:

    • 你是最棒的!实际上,我不得不将其更改为 GrantTypes.ImplicitAndClientCredentials,但这确实起到了作用。谢谢!
    猜你喜欢
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2018-04-28
    • 2021-01-27
    相关资源
    最近更新 更多