打字稿中的 Azure 函数验证令牌

Azure function validate token in typescript

我正在使用 Azure Static Web App 构建网站,login/registration 由 Azure B2C 处理。 API 是打字稿 Azure 函数(Azure 静态 Web 应用程序的一部分)。我有一些 API 调用仅在用户登录时访问,因此我将不记名令牌发送到后端。

我如何在后端(打字稿)针对我的 Azure B2C 租户验证此令牌,以便在其有效时将数据发回给用户?

如果您想验证 Azure AD B2C 访问令牌,我们可以使用包 azure-ad-verify-token 来实现它

例如

import { verify, VerifyOptions } from 'azure-ad-verify-token';
 
const options: VerifyOptions = {
  jwksUri: 'https://<tenantName>.b2clogin.com/<tenantName>.onmicrosoft.com/discovery/v2.0/keys?p=b2c_1_signupsignin1',
  issuer: 'https://<tenantName>.b2clogin.com/<tennat id>/v2.0/',
  audience: '<Identifies the intended recipient of the token. For Azure AD B2C, the audience is the application ID>'
};
var res = await verify(token, options);
 context.log(res);

详情请参考here and here