代码之家  ›  专栏  ›  技术社区  ›  Mathieu

IdentityServer令牌颁发者声称颁发的JWT令牌中缺少端口号

  •  0
  • Mathieu  · 技术社区  · 8 月前

    我的本地开发IdentityServer发行的令牌有一个奇怪的问题(IdentityServer4和Duende IdentityServer都会发生这种情况)。

    我的本地开发计算机上的IdentityServer正在端口44310上运行,这在发现文档中得到了正确反映

    enter image description here

    然而,几天后,当我获得JWT访问令牌时,由于某种原因,发行者只是https://localhost当我在jwt.io中检查它时

    enter image description here

    因此,它省略了端口,因此,当我从调用客户端使用此令牌时,它会返回Unauthorized和消息https://localhost是无效的发行者。

    奇怪的是,这种情况几天前才开始发生(无法准确确定何时发生),因为我的本地IdentityServer代码几个月来没有变化。

    更奇怪的是,在另一个开发VM上,使用完全相同的Visual Studio 2022版本并运行与我的IdentityServer项目完全相同的代码库,它仍然正常工作(即发出发卡行)https://localhost:44310“在JWT令牌中)。

    有什么想法吗?

    编辑:

    Tore的回答引起了我的思考,我决定从Postman调用发现端点,事实上,从那里,发卡机构被返回为https://localhost没有端口,而被调用的url实际上有端口,我不明白为什么从浏览器调用它时会有所不同(以及最近是什么让它改变了这种行为)

    enter image description here

    2 回复  |  直到 8 月前
        1
  •  1
  •   Tore Nestenius    8 月前

    发现文档中的URL将根据谁在调用IdentityServer而保持警惕,因此当客户端调用Identity服务器和浏览器调用它时,您将看到不同的发布者。

    一种选择是将发行者设置为静态URL,如下所示:

    var isBuilder = builder.Services.AddIdentityServer(options =>
    {
        options.IssuerUri = "https://identity";
        ...
    }
    

    您可能还需要覆盖客户端中使用的URL,如下所示:

    }).AddOpenIdConnect(options =>
    {
        options.Authority = "https://localhost:7001";
        options.MetadataAddress = "http://identity:80/.well-known/openid-configuration";
    
        ...
        options.Events.OnRedirectToIdentityProvider = context =>
        {
            context.ProtocolMessage.IssuerAddress = "https://localhost:7001/connect/authorize";
            return Task.CompletedTask;
        };
    });
    

    你可以在我的博客文章中阅读完整的解释: IdentityServer in Docker Containers

        2
  •  0
  •   Mathieu    8 月前

    发现问题!

    我在设置中的Postman HTTP版本设置为Auto,我想它使用HTTP 2。

    当我硬选择HTTP 2时,它确实没有端口,但当我把它放在HTTP 1.x上时,端口就包括在内了。。。想想看吧

    enter image description here

    enter image description here