identityserver4 - 来自 4.0.0 版文档的示例中的 Identity Server 无效范围
问题描述
我正在使用 IdentityServer4,遵循 https://identityserver4.readthedocs.io/en/latest/quickstarts/1_client_credentials.html上的文档
但是我在使用客户端凭据请求令牌时invalid_scope
使用的客户端出现错误。IdentityModel
我可能错过了一些步骤,但我已经复习了好几次。
奇怪的是,身份服务器端点显示以下日志:
Invalid scopes requested, {"ClientId": "client", "ClientName": null, "GrantType": "client_credentials", "Scopes": null, "AuthorizationCode": null, "RefreshToken": null, "UserName": null, "AuthenticationContextReferenceClasses": null, "Tenant": null, "IdP": null, "Raw": {"grant_type": "client_credentials", "scope": "api1", "client_id": "client", "client_secret": "***REDACTED***"}, "$type": "TokenRequestValidationLog"}
Scopes
那岂不是很奇怪,null
以后scope
才有api1
价值?
我正在使用内存值。
public static class Config
{
public static IEnumerable<IdentityResource> Ids =>
new IdentityResource[]
{
new IdentityResources.OpenId()
};
public static IEnumerable<ApiResource> Apis =>
new List<ApiResource>
{
new ApiResource("api1", "My Api")
};
public static IEnumerable<Client> Clients =>
new List<Client>
{
new Client
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
}
};
}
和
public void ConfigureServices(IServiceCollection services)
{
// uncomment, if you want to add an MVC-based UI
//services.AddControllersWithViews();
var builder =
services
.AddIdentityServer()
.AddInMemoryApiResources(Config.Apis)
.AddInMemoryClients(Config.Clients)
.AddInMemoryIdentityResources(Config.Ids);
// not recommended for production - you need to store your key material somewhere secure
builder.AddDeveloperSigningCredential();
}
public void Configure(IApplicationBuilder app)
{
if (Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// uncomment if you want to add MVC
//app.UseStaticFiles();
//app.UseRouting();
app.UseIdentityServer();
// uncomment, if you want to add MVC
//app.UseAuthorization();
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapDefaultControllerRoute();
//});
}
我可以看到众所周知的配置,尽管它没有提到 api1 作为支持的范围。
这是客户端
var client = new HttpClient();
var discovery =
await client.GetDiscoveryDocumentAsync("https://localhost:5001");
if (discovery.IsError)
{
await Console.Out.WriteLineAsync("Discovery error");
return;
}
// request token
var clientCredentialsTokenRequest =
new ClientCredentialsTokenRequest
{
Address = discovery.TokenEndpoint,
ClientId = "client",
ClientSecret = "secret",
Scope = "api1"
};
var tokenResponse =
await client.RequestClientCredentialsTokenAsync(clientCredentialsTokenRequest);
我是否缺少任何额外的东西来拥有最基本的样本工作?
更新 1:
好的,我已将 Identity Server 降级到 3.1.3,它可以正常工作。对于 Identity Server 4.0.0 版本,必须进行一些更改。会在那里调查。
解决方案
发现了一个问题,为我指明了正确的方向。通过用 ApiScopes 替换 ApiResources 来修复它:
public static IEnumerable<ApiScope> Apis =>
new List<ApiScope>
{
new ApiScope("api1", "My Api")
};
和
var builder =
services
.AddIdentityServer()
.AddInMemoryApiScopes(Config.Apis)
//.AddInMemoryApiResources(Config.Apis) //OLD?
.AddInMemoryClients(Config.Clients)
.AddInMemoryIdentityResources(Config.Ids);
我想文档还没有更新。
在尝试访问受保护的 Api 时,我仍然会遇到未经授权的情况,但那是另一回事。
推荐阅读
- r - 如何在 cosinor2 上添加标准误差?
- python - 在内存中生成一个大文件并将其流式传输到对象存储中
- hazelcast - Hazelcast Wan 复制默认模式
- r - R - 将矩阵写入带有标题和脚注的输出文件
- r - R Shiny Leaflet Maps 中的 setView() 字典
- flutter - Error when selecting a dropdown item in Flutter
- python - Differentiating between a python sibling import vs importing a function/class
- c++ - 为什么我的 main.cpp 不打印头文件中的任何内容
- nginx - 将 .Net Core Web API 托管到 nginx(不是代理)
- python - 获取 post 方法的 json