版本 4.0.0 文档中的示例中的 Identity Server 范围无效

2024-01-10

我正在使用 IdentityServer4,遵循以下文档https://identityserver4.readthedocs.io/en/latest/quickstarts/1_client_credentials.html 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 is null以及后来scopeapi1 value?

我正在使用内存值。

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" }
            }
        };
    
}

and

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 版本,某些内容必须发生变化。会去那里调查。


找到一个issue https://github.com/IdentityServer/IdentityServer4/issues/4441这为我指明了正确的方向。通过将 ApiResources 替换为 ApiScopes 来修复此问题:

public static IEnumerable<ApiScope> Apis =>
    new List<ApiScope>
    {
        new ApiScope("api1", "My Api")
    };

and

var builder =
    services
        .AddIdentityServer()
        .AddInMemoryApiScopes(Config.Apis)
        //.AddInMemoryApiResources(Config.Apis) //OLD?
        .AddInMemoryClients(Config.Clients)
        .AddInMemoryIdentityResources(Config.Ids);

我认为文档尚未更新。

当我尝试访问受保护的 Api 时,仍然遇到未经授权的情况,但那是另一回事了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

版本 4.0.0 文档中的示例中的 Identity Server 范围无效 的相关文章

随机推荐

  • NHibernate 警告并且数据未保存

    当我通过调用 SaveOrUpdate 进行保存时 收到此警告 并且在调用 Transaction Commit 后数据未保存在数据库中 NHibernate Engine ForeignKeys 无法 确定 项目名称 是否与 分配的标识符
  • Spark DataFrame 重新分区和 Parquet 分区

    我在列上使用重新分区来将数据存储在镶木地板中 但 我看到没有 parquet 分区文件的数量与 不 Rdd 分区 rdd分区之间是否没有相关性 和镶木地板隔断 当我将数据写入镶木地板分区并使用 Rdd 时 重新分区 然后我从 parquet
  • 带有 C# 内部访问修饰符的 Doxygen

    我正在使用 Doxygen 为我正在处理的 C 项目生成一些 API 文档 我在这个项目中有相当多的 内部 功能 并且不希望 Doxygen 在它生成的生成的 html 中生成这些签名 我已尝试启用 HIDE FRIEND COMPOUND
  • macOS 中的 UIGraphicsBeginImageContextWithOptions 模拟

    以下是我用来在 iOS 中缩放图像的代码 即将 500x500 图像缩放到 100x100 图像 然后存储缩放后的副本 UIImage image UIImage originalImage scaledToSize CGSize desi
  • NaN 作为字典中的键

    谁能向我解释以下行为 gt gt gt import numpy as np gt gt gt np nan 5 np nan 5 gt gt gt float64 np nan 5 float64 np nan KeyError nan
  • 切换到共享运行时后 Office 插件加载失败

    我跟着配置您的 Office 加载项以使用共享 JavaScript 运行时 https learn microsoft com en us office dev add ins develop configure your add in
  • 如何使用searchkick根据某些条件进行索引

    我正在使用searchkick 和rails4 我有一个活动记录 People 其属性为 a b c 如何仅当 b 等于 type1 时才建立索引 否则不建立索引 目前我所知道的是 def search data a a b b c c e
  • java等待光标显示问题

    我在应用程序中显示等待光标时遇到问题 只要鼠标位于定义其自身光标的面板上方 等待光标就不会出现 如果面板不改变光标 则会出现等待光标 我附上 SSCE 来准确解释我的问题 public class BusyCursorTest extend
  • VS 2017 中的 MySQL 和 MVC 实体框架无法正常工作

    我正在尝试启动 MVC EF Visual Studio 2017 项目 我在本地实例上使用 MySQL 设置了数据连接 但是当我创建 ADO net 数据模型时 出现图中所示的错误 这里还有另一篇文章 无法对实体框架 6 使用 MySQL
  • 对数据库的访问进行队列以避免多个缓存项

    我有一个与音乐相关的 ASP NET 网站 它在第一次请求时缓存数据库中的大量静态信息 有时 应用程序负载较重时会重置应用程序并清除缓存 然后所有 http 请求都会转到数据库以检索静态数据并将其缓存以供其他请求使用 如何确保只有一个请求进
  • 表达式树生成的 IL 是否经过优化?

    好吧 这只是好奇心 对现实世界没有帮助 我知道使用表达式树 您可以像常规 C 编译器一样即时生成 MSIL 由于编译器可以决定优化 我很想问在执行期间生成的 IL 的情况如何Expression Compile 基本上有两个问题 因为在编译
  • ResizeObserver 一 vs 多性能

    The 调整大小观察者 https wicg github io ResizeObserver has an 观察 https wicg github io ResizeObserver dom resizeobserver observe
  • 保留源 shell 脚本而不退出终端

    我正在编写一个 shell 脚本来保存一些击键并避免拼写错误 我想将脚本保留为单个文件 该文件调用内部方法 函数并在出现问题时终止函数without离开航站楼 my script sh bin bash exit if no git if
  • 为什么方法定义返回符号?

    当你定义一个方法时 它会返回一个与该方法同名的符号 这有道理吗 或者它只是作为您创建它的验证 Like so def something end gt something IRb always显示调用结果inspect计算的最后一个表达式的
  • StringBuilder容量()

    我注意到capacity方法返回StringBuilder没有逻辑的能力 方式 有时它的值等于字符串长度 有时它更大 有没有一个方程式可以知道它的逻辑是什么 我将尝试用一些例子来解释这一点 public class StringBuilde
  • 编译错误:“stddef.h:没有这样的文件或目录”

    每当我尝试编译此代码时 总是会出现以下错误 In file included from usr include wchar h 6 0 from usr lib gcc i686 pc cygwin 4 9 2 include c cwch
  • 在 Inno Setup 中将字符串编码为 Base64(Inno Setup 的 Unicode 版本)

    Problem 我尝试使用帕斯卡函数EncodeStringBase64 假设 Inno Setup 可以访问 Pascal 标准库 但它无法找到它并提供一个Unknown Identifier error https www freepa
  • 使用 GDI+ 将修改后的图像保存到原始文件

    我正在从文件加载位图图像 当我尝试将图像保存到另一个文件时 出现以下错误 GDI 中发生一般错误 我相信这是因为文件被图像对象锁定 好的 所以尝试调用 Image Clone 函数 这仍然锁定文件 唔 接下来 我尝试从 FileStream
  • 删除目录下的所有文件

    我需要使用 Qt 删除目录中的所有文件 该目录中的所有文件都将具有扩展名 txt 我不想删除目录本身 有谁知道我该怎么做 我看过 QDir 但没有运气 比约恩斯的答案被调整为不会永远循环 QString path whatever QDir
  • 版本 4.0.0 文档中的示例中的 Identity Server 范围无效

    我正在使用 IdentityServer4 遵循以下文档https identityserver4 readthedocs io en latest quickstarts 1 client credentials html https i