azure - 尽管 token 正确,但用户未经授权

标签 azure odata dynamics-365-operations dynamics-ax7

我正在尝试将 D365FO 与第三方应用程序集成,我能够进行正确的设置并注册我的应用程序,获取 token ,如下所示:

enter image description here

我在开发机器上使用资源作为D365FO的链接,即https://usnconeboxax1aos.cloud.onebox.dynamics.com/并阅读D365FO数据如下图:

enter image description here

我想更改登录方法,因此我使用密码代表用户登录,因此我禁用了 Azure 上的安全详细信息(基于 this ),如下所示:

enter image description here

并成功获取登录用户的 token ,如下所示:

enter image description here

enter image description here

但是,当我尝试获取之前能够获取的相同数据时,尽管我使用 Admin 帐户登录,但还是收到了 401 未经授权的错误:

enter image description here

最佳答案

如果您调用具有无效受众的资源,通常会出现401 Unauthorized错误。

当您生成 scopeuser.read openid profileoffline_access 的访问 token 时,受众将为 Microsoft Graph,这将不起作用与 D365FO。

我尝试通过 Postman 在我的环境中重现相同的结果,并得到以下结果:

我注册了一个 Azure AD 应用程序并添加了相同的 API 权限,如下所示:

enter image description here

现在,我通过包含相同参数,生成了授予类型为password的 token ,如下所示:

POST https://login.microsoftonline.com/organizations/oauth2/v2.0/token

client_id: <appID>
client_secret: <secret>
scope: user.read openid profile offline_access
grant_type: password
username: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcbdb8b1b5b29ca4a4a4a4a4a4a4a4a4f2b3b2b1b5bfaeb3afb3baa8f2bfb3b1" rel="noreferrer noopener nofollow">[email protected]</a>
password: xxxxxxxxxxx

回应:

enter image description here

您可以通过将上述访问 token 粘贴到 jwt.ms 来解码以检查受众。

当我解码访问 token 时,我得到 aud 声明为 00000003-0000-0000-c000-000000000000(即 Microsoft Graph),如下所示:

enter image description here

如果您使用此 token 读取 D365FO 数据,您将收到 401 Unauthorized 错误,因为受众无效。

解决该错误,您需要通过进行以下更改来生成访问 token ,其中 resource 值作为 D365FO 实例的基本 URL:

POST https://login.microsoftonline.com/organizations/oauth2/token

client_id: <appID>
client_secret: <secret>
resource: <base URL of your D365FO instance without the trailing '/'>
grant_type: password
username: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0160656c686f417979797979797979792f6e6f6c6862736e726e67752f626e6c" rel="noreferrer noopener nofollow">[email protected]</a>
password: xxxxxxxxxxx

在您的情况下,资源参数的值应为https://usnconeboxax1aos.cloud.onebox.dynamics.com

此 token 的受众与您的 D365FO 根 URL 相同。为了确认这一点,您可以在 jwt.ms 中对其进行解码。如果你用这个token读取D365FO数据,就可以了!

引用:

Test services by using third-party utilities - Finance & Operations | Dynamics 365 | Microsoft

关于azure - 尽管 token 正确,但用户未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75274922/

相关文章:

Azure Databricks 和日志分析设置需要重新启动或重建吗?

asp.net-web-api - 如何将两个不同的 Odata 路由分派(dispatch)到同一个 EntitySetController<TEntity, TKey>?

architecture - OData 是否仅用作向其他方发布数据的方式,或者我可以将其用作我的应用程序的数据层吗

axapta - 可以修改或扩展 D365FO 中的代码以抑制抛出的错误

axapta - D365FO 中的命令链(方法包装)结果为 'Object is not set to an instance of an object'

sql-server - 分析服务连接被远程主机关闭(Azure SQL 数据库)

azure - 通过命令行错误将Web应用程序发布到Azure

c# - OData WebAPI 2 复杂授权

axapta - While select with if 语句语法 - if 语句的目的是什么?

c# - 使用 SAS 生成 blob sasurl