identityserver4 - 抱歉,出现错误:未授权_客户端客户端的授权类型无效
问题描述
在我的解决方案中,我有 3 个项目:
- 身份服务器 4
- ASP.NET Core 3.1 中的 Web 客户端
- ASP.NET Core 3.1 中的 Web Api
到目前为止,我设法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 missing
和Request 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
。
解决方案
当您收到“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。
推荐阅读
- r - 尝试在 R 中加载“stats”包时,包或命名空间加载失败
- python - 如何在多个端口独立服务 CherryPy 应用程序?
- node.js - Cloudflare SSL:在 SSL 应用后 Express 不工作
- java - 在Java中以年份数据的形式制作月份数据
- excel - 在 ActiveCell.FormualR1C1 中传递变量
- javascript - 当我单击 td 标签(javascript + html)时,我无法将输入标签添加到 td 标签中
- angular - Protractor-cucumber :在断言失败后继续执行剩余的步骤,而不是跳过这些步骤,
- database - 必须设置获取 ORACLE_HOME 并且 %ORACLE_HOME%\database 必须是可写的 createdb 在 postgres 中创建数据库时未成功终止
- php - 在查询中使用 Like 和 SQL 注入的影响
- reactjs - 在 React Native 屏幕之间传递数据(从屏幕 A 到屏幕 B 到屏幕 C)