【问题标题】:Wrong audience for JWT using ADAL.js使用 ADAL.js 的 JWT 的错误受众
【发布时间】:2018-02-26 08:11:37
【问题描述】:

我在我的 SPA 中使用 adal-angular 库(但不使用 Angular)来尝试获取可用于调用 SharePoint API (https://<my-tenant>.sharepoint.com/_api/) 的访问令牌。

我在 Azure AD 中注册了一个应用程序并在清单中启用了隐式流,我现在在本地运行我的 SPA,这就是为什么有一个 localhost 重定向 URI。下面的代码是在启动时执行的:

const context = new AuthenticationContext({
    clientId: '<my-client-id>',
    redirectUri: 'http://localhost:3000/signin',
    popUp: true,
    loginResource: 'https://<my-tenant>.sharepoint.com',
    callback: () => window.location.reload()
});

const user = context.getCachedUser();
if (!user) {
    context.login();
} else {
    context.acquireToken('https://<my-tenant>.sharepoint.com', (error, token) => {
        console.log(error, token);
    });
}

我已经登录到 SharePoint 网站,因此通过此配置,一切都会自动发生,并且我看到一个 JWT 访问令牌记录到控制台。但是,当我检查令牌时,我看到观众是&lt;my-client-id&gt;。使用访问令牌调用 https://&lt;my-tenant&gt;.sharepoint.com/_api/v1.0/me 时,我会收到 401 响应,并显示以下错误消息:

{"error_description": "Exception of type 'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException' was thrown."}

我很确定这一切都归结为我没有正确理解 OAuth2 流程,但是...我如何才能获得 SharePoint 可以实际与 SharePoint 一起使用的令牌?我在想这个错误的方式吗?如果我的应用程序检索到的令牌只能用于对我自己的应用程序进行身份验证,这有点违背了目的。

【问题讨论】:

  • 您将对 SharePoint API 进行哪些类型的调用?您是否考虑过改用 Microsoft Graph(它确实可以访问您的 Sharepoint 资源)并且有更多的文档和示例代码来说明如何调用它。
  • 嗯,不,我没有考虑到这一点。它可能会做我需要的事情。但是令牌受众呢,我不会有同样的问题吗?
  • 您可以尝试在资源网址的末尾添加/ 吗?
  • 我现在尝试在 loginResource 和请求的资源的末尾添加正斜杠,但没有任何区别 - 仍然收到相同的受众验证错误。
  • 您的 error_description 是否附加了其他错误消息?仅凭这一点,很难说问题是什么。通常令牌端点会给出比这更好的错误消息。

标签: javascript oauth-2.0 azure-active-directory adal adal.js


【解决方案1】:

此处详细描述了获取 SharePoint 的访问令牌:

OneDrive for Business authentication and sign in

您应该考虑首先获取到 Discovery Endpoint 的令牌:

使用接收到的资源访问令牌 https://api.office.com/discovery/您可以向 发现 API 以了解哪些服务可用

如果调用成功,则响应正文包含 JSON 数据 有关用户和您的应用可用的服务的信息。

{
  "@odata.context": "https:\/\/api.office.com\/discovery\/v1.0\/me\/$metadata#allServices",
  "value": [
    {
      "@odata.type": "#Microsoft.DiscoveryServices.ServiceInfo",
      "capability": "MyFiles",
      "serviceApiVersion": "v2.0",
      "serviceEndpointUri": "https:\/\/contoso-my.sharepoint.com\/_api\/v2.0",
      "serviceResourceId": "https:\/\/contoso-my.sharepoint.com\/"
    }
  ]
}

您应该在此处获得有效的资源 ID...但这里的问题可能只是您没有在示例代码中的资源 URL 末尾包含正斜杠 (/)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 2020-08-10
    • 2015-01-06
    • 2021-06-16
    • 2016-12-23
    相关资源
    最近更新 更多