了解 Oauth 2.0 授权代码流程

Understanding Oath 2.0 Autorization Code Flow

为什么我们需要先联系 Oath Auth 端点以获取授权码,然后在收到授权码后我们需要再次联系 Oauth Auth 端点以获取访问令牌,以便我们可以调用网络服务?

为什么不直接 return 在用户成功登录后第一步中的访问令牌?

此外,网络服务(API)如何验证访问令牌是否合法?

Why do we need first contact the Oath Auth endpoint to get an auth code, and then once we have received the auth code we need to contact the Oauth Auth endpoint again to get the access token so that we can call a webservice?

因此 Web 服务(或依赖方)永远看不到用户的凭据。 由于此流程的工作方式,用户也看不到应用程序的凭据。 用户也无法获得访问令牌来自己使用它,尽管这实际上并没有那么重要。隐式授予流程实际上可以满足您的需求,允许您直接从授权端点获取访问令牌。但这主要针对单页应用程序,这是最简单的选择。 授权代码授予流程允许应用程序通过客户端密码或证书使用更强的身份验证。

顺便说一句,这叫做 OAuth 舞蹈 :)

Why not just return the access token in the first step, after the user has signed in successfully?

参见我上面提到的隐式授权流程。

Also, how does the webservice (API) then verify that the access token is legit?

通过检查数字签名。 Azure AD(和 B2C)为他们用于在众所周知的端点签名的密钥对发布 public 密钥。 应用程序中的身份验证部分必须通过定义的 public 密钥检查 JWT 签名是否有效。