是否允许本机应用程序客户端在重定向 URI 中使用任何随机可用端口?

Is it allowed for a Native App client to use any random available port in a redirect URI?

我正在了解如何以正确的方式为 Windows 实施 OAuth 2.0 本机应用程序。我偶然发现了这个用 C# 编写的 example。 但这就是这个例子让我烦恼的地方:

// Creates a redirect URI using an available port on the loopback address.
string redirectUri = $"http://{IPAddress.Loopback}:{GetRandomUnusedPort()}/";

我读到的有关 OAuth 2.0 的任何地方都明确指出 redirect_uri 必须在 OAuth 服务器上预先注册并且不能是动态的。 示例是错误的还是我遗漏了什么?

根据 OAuth for Native Apps,您应该能够像这样注册一个静态重定向 URI:

  • http:///localhost/callback

然后在运行时,您应该能够像这样使用各种重定向 URL:

  • http://localhost:8001/callback
  • http://localhost:8002/callback
  • http://localhost:8003/callback

虽然我很少看到这种支持,但更常见的是您需要注册三个左右的特定 URL。您需要测试您的提供商才能确定。

感谢 Gary 的回复和他的 link 提供,我找到了这篇文章:

The authorization server MUST allow any port to be specified at the time of the request for loopback IP redirect URIs, to accommodate clients that obtain an available ephemeral port from the operating system at the time of the request.

在我原来的 post 中,我对使用随机可用端口的示例感到困惑,该端口使 redirect-uri 动态化。 但事实证明,oauth 服务器 必须 允许来自客户端的 redirect-uri 的任何端口,只要它位于环回接口上。