Identityserver4 抱歉,出现错误:未经授权的\u客户端客户端的授权类型无效

Identityserver4 抱歉,出现错误:未经授权的\u客户端客户端的授权类型无效,identityserver4,Identityserver4,在我的解决方案中,我有3个项目: 身份服务器4 ASP.NET Core 3.1中的Web客户端 ASP.NET Core 3.1中的Web Api 到目前为止,我设法从web客户端获取id令牌,但在添加另一个API项目并尝试访问需要Identity Server 4授权才能获取访问令牌的API项目后,我收到了此错误抱歉,出现错误:当我单击“登录到Identity server”时,客户端的授权类型无效。我可以知道如何排除此错误吗 这是连接到Identity Server的myStartup

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

  • 身份服务器4
  • ASP.NET Core 3.1中的Web客户端
  • ASP.NET Core 3.1中的Web Api
到目前为止,我设法从web客户端获取
id令牌
,但在添加另一个API项目并尝试访问需要Identity Server 4授权才能获取
访问令牌的API项目后,我收到了此错误
抱歉,出现错误:当我单击“登录到Identity server”时,客户端的授权类型无效。我可以知道如何排除此错误吗

这是连接到Identity Server的my
Startup
类中的当前配置

    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\u token”带有
options.ResponseType=“code”但仍然与上面的错误相同。xxx是我使用powershell生成的测试guid

在我的身份服务器
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
                },
            };
    }
公共静态类配置
{
公共静态IEnumerable IdentityResources=>
新标识资源[]
{
新标识资源.OpenId(),
新标识资源.Profile(),
};
公共静态IEnumerable ApiScopes=>
新安培镜[]
{
新ApiScope(“scope1”),
新ApiScope(“scope2”),
};
公共静态IEnumerable ApiResources=>
新资源[]
{
新的ApiResource(“movie.api”,“电影api”)
{
作用域={“movie.api”}
}
};
公共静态IEnumerable客户端=>
新客户[]
{
//m2m客户端凭据流客户端
新客户
{
ClientId=“m2m.client”,
ClientName=“客户端凭据客户端”,
AllowedGrantTypes=GrantTypes.ClientCredentials,
ClientSecrets={newsecret(“511536EF-F270-4058-80CA-1C89C192F69A”.Sha256())},
AllowedScopes={“scope1”}
},
//使用码流+pkce的交互式客户端
新客户
{
ClientId=“交互式”,
ClientSecrets={newsecret(“49C1A7E1-0C79-4A89-A3D6-A37998FB86B0”.Sha256())},
AllowedGrantTypes=GrantTypes.Code,
重定向URI={”https://localhost:44300/signin-oidc“},
FrontChannelLogoutUri=”https://localhost:44300/signout-oidc“,
PostLogoutRedirectUris={”https://localhost:44300/signout-回调oidc“},
AllowOfflineAccess=true,
AllowedScopes={“openid”、“profile”、“scope2”}
},
新客户
{
ClientId=“movie.web”,
ClientSecrets={newsecret(“xxx.Sha256())},
AllowedGrantTypes=GrantTypes.Hybrid,
重定向URI={”http://localhost:5000/signin-oidc“},
AllowedScopes={“openid”、“profile”、“movie.api”},
AllowAccessTokensViaBrowser=true
},
};
}
在我的控制台中,我注意到以下信息:

code\u质询缺失
请求验证失败

我应该在哪里检查这些

如果我设置为
options.ResponseType=“code id\u token”,在我的控制台中,我将获得
code\u缺少挑战

如果我设置为
options.ResponseType=“code”,在我的控制台中,我将获得客户的
无效授权类型:授权\u代码

我有
builder.AddInMemoryClients(Config.Clients)的疑难解答和服务器中的
ClientSecrets
与客户端在
options.ClientSecret=xxx
处匹配当您收到“code\u 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仅支持授权代码流。

当您收到“code\u 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仅支持授权代码流。

我已尝试使用`options.ResponseType=“code”;options.UsePkce=true;`但是当我加载登录名时,它会说:
对不起,出现了一个错误:未经授权的客户端无效授权类型客户端
,在控制台日志中,
客户端无效授权类型:授权\u代码
我尝试了你的解决方案,但我仍然得到了客户端无效授权类型:授权\u代码
。顺便说一下,我的clientid
movie.api
设置为
AllowedGrantTypes=GrantTypes.Hybrid,
。因此,我的响应类型不是
code-id\u-token
,而是更新后的答案。为什么需要混合流?今日作者