无法获取访问令牌。 'AADSTS500011:在租户 -tenantid 中找不到名为 'xxx' 的资源主体

Unable to get access token. 'AADSTS500011: The resource principal named 'xxx' was not found in the tenant -tenantid

我正在尝试获取 Azure 函数应用程序的访问令牌。我已经为功能应用程序(系统分配)启用了托管标识。但是在使用 nuget Azure.Identity.

获取令牌时

var tokenCredential = new DefaultAzureCredential(); var accessToken = await tokenCredential.GetTokenAsync( new TokenRequestContext(scopes: new string[] { "https://xxx.azure-api.net/" + "/.default" }) { } );

我遇到了错误。

The resource principal named 'xxx.azure-api.net' was not found in the tenant 123

但是当运行az cli查看订阅详细信息时,订阅确实是租户123的一部分而已。

您需要在 azure ad 中注册应用程序并启用访问令牌。完成后,您需要提供对 xxx.azurewebsites.net

的 RBAC 访问权限

按照本文获取分步文档 Microsoft Document Reference

这是我最终完成的。

  1. 我已经在 AD 中注册了一个 App。并公开了该应用程序的 API。
  2. 我已将系统分配的托管标识分配给函数。
  3. 在本地我无法请求令牌,因为 Azure CLI 未获得许可。
  4. 在 Function 中部署应用程序后,我的 Function 应用程序可以使用其身份请求令牌。