Sorry, there was an error : unauthorized_client Invalid grant type for client
Sorry, there was an error : unauthorized_client Invalid grant type for client
在我的解决方案中,我有 3 个项目:
- 身份服务器 4
- ASP.NET Core 3.1 中的 Web 客户端
- Web Api ASP.NET Core 3.1
到目前为止,我设法从 Web 客户端获取了 id token
,但是在添加了另一个 API 项目并尝试访问需要 Identity Server 4 授权才能获取的 API 项目之后access token
,当我单击登录到身份服务器时出现此错误 Sorry, there was an error : unauthorized_client Invalid grant type for client
。请问我应该如何解决这个错误?
这是我的 Startup
class 中连接到 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";
替换options.ResponseType = "code id_token";
,但仍然出现与上述相同的错误。 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
},
};
}
在我的控制台中,我注意到这些信息:
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”错误时,是因为您的客户端不包含以下两个 headers:
&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 仅支持授权代码流。
在我的解决方案中,我有 3 个项目:
- 身份服务器 4
- ASP.NET Core 3.1 中的 Web 客户端
- Web Api ASP.NET Core 3.1
到目前为止,我设法从 Web 客户端获取了 id token
,但是在添加了另一个 API 项目并尝试访问需要 Identity Server 4 授权才能获取的 API 项目之后access token
,当我单击登录到身份服务器时出现此错误 Sorry, there was an error : unauthorized_client Invalid grant type for client
。请问我应该如何解决这个错误?
这是我的 Startup
class 中连接到 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";
替换options.ResponseType = "code id_token";
,但仍然出现与上述相同的错误。 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
},
};
}
在我的控制台中,我注意到这些信息:
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”错误时,是因为您的客户端不包含以下两个 headers:
&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 仅支持授权代码流。