首页 > 解决方案 > 抱歉,出现错误:未授权_客户端客户端的授权类型无效

问题描述

在我的解决方案中,我有 3 个项目:

到目前为止,我设法id token从 Web 客户端获取,但是在添加了另一个 API 项目并尝试访问需要从 Identity Server 4 获得授权的 API 项目后,当我单击登录到身份服务器时access token出现此错误。Sorry, there was an error : unauthorized_client Invalid grant type for client我可以知道如何解决此错误吗?

这是我Startup班级中连接到 Identity Server 的当前配置。

    private void SetOpenIdConnectOptions(OpenIdConnectOptions options)
    {
        options.Authority = "https://localhost:5001";
        options.ClientId = "movie.web"; 
        options.RequireHttpsMetadata = false;
        options.Scope.Add("profile");
        options.Scope.Add("openid");
        options.Scope.Add("movie.api");
        options.ResponseType = "code id_token";
        options.SaveTokens = true;
        options.ClientSecret = "xxx";
    }

我试图替换options.ResponseType = "code id_token";options.ResponseType = "code";但仍然与上述相同的错误。那xxx是我使用 powershell 生成的测试指南。

在我的身份服务器中config.cs

public static class Config
    {
        public static IEnumerable<IdentityResource> IdentityResources =>
            new IdentityResource[]
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
            };


        public static IEnumerable<ApiScope> ApiScopes =>
            new ApiScope[]
            {
                new ApiScope("scope1"),
                new ApiScope("scope2"),
            };

        public static IEnumerable<ApiResource> ApiResources =>
           new ApiResource[]
           {
                new ApiResource("movie.api", "The Movie API")
                {
                    Scopes = { "movie.api" }
                }
           };

        public static IEnumerable<Client> Clients =>
            new Client[]
            {
                // m2m client credentials flow client
                new Client
                {
                    ClientId = "m2m.client",
                    ClientName = "Client Credentials Client",

                    AllowedGrantTypes = GrantTypes.ClientCredentials,
                    ClientSecrets = { new Secret("511536EF-F270-4058-80CA-1C89C192F69A".Sha256()) },

                    AllowedScopes = { "scope1" }
                },

                // interactive client using code flow + pkce
                new Client
                {
                    ClientId = "interactive",
                    ClientSecrets = { new Secret("49C1A7E1-0C79-4A89-A3D6-A37998FB86B0".Sha256()) },
                    
                    AllowedGrantTypes = GrantTypes.Code,

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

                    AllowOfflineAccess = true,
                    AllowedScopes = { "openid", "profile", "scope2" }
                },

                new Client
                {
                    ClientId = "movie.web",

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

                    AllowedGrantTypes = GrantTypes.Hybrid,

                    RedirectUris = { "http://localhost:5000/signin-oidc" },
     
                    AllowedScopes = { "openid", "profile", "movie.api" },
                    AllowAccessTokensViaBrowser =  true
                },
            };
    }

在我的控制台中,我注意到以下信息:

code_challenge is missingRequest validation failed

我应该在哪里检查这些?

如果我设置为options.ResponseType = "code id_token";,在我的控制台中,我会得到code_challenge is missing

如果我设置为options.ResponseType = "code";,在我的控制台中,我会得到Invalid grant type for client: authorization_code

我在服务器中进行了故障排除builder.AddInMemoryClients(Config.Clients);,并且ClientSecrets在服务器中与客户端匹配options.ClientSecret = xxx

标签: identityserver4

解决方案


当您收到“code_challenge is missing”错误时,这是​​因为您的客户端不包含以下两个标头:

&code_challenge=SD3BJSDKJ215KZAF...
&code_challenge_method=S256

在客户端中确保此选项设置为 true:

options.UsePkce = true;

PKCE是对授权代码流的安全增强。在 IdentityServer v4.0x 中,RequirePkce选项现在也默认设置为 true。

对于另一个问题,您应该使用

response_type = "code",

在 IdentityServer 客户端定义中,您应该使用:

AllowedGrantTypes = GrantTypes.Code,

或者,如果您需要多个流程:

AllowedGrantTypes = 
{
    GrantType.AuthorizationCode,
    GrantType.Hybrid
},

但请记住,仅授权代码流支持 PKCE。


推荐阅读