在我的解决方案中,我有3个项目:
到目前为止,我设法从Web客户端获取了id token
,但是在添加另一个API项目并尝试访问需要获得Identity Server 4授权才能获得access token
的API项目之后,出现了此错误当我单击登录到身份服务器时的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生成的测试Guid。
在我的Identity Server 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
上的客户端匹配。
答案 0 :(得分:1)
当您收到“缺少code_challenge”错误时,原因是您的客户端不包含以下两个标头:
&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。