为什么我的 Azure 地图控件的匿名身份验证不起作用?

Why isn't Anonymous authentication for my Azure maps control working?

我正在开发一个单页应用程序,并且正在尝试设置匿名身份验证 here。我在本地有一个 Azure Function 运行 来为令牌调用令牌服务提供者:

 public static class TokenService
    {
        [FunctionName("TokenService")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger TokenService function processed a request.");

            var azureServiceTokenProvider = new AzureServiceTokenProvider();
            string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://atlas.microsoft.com");
            return new OkObjectResult(accessToken);
        }
    }

我正在使用我的(基于 MSA 的)Azure 登录帐户作为我本地开发机器上的身份。通过 Postman returns 一个 200 状态和一个令牌在本地调用函数端点,所以这看起来很好。

在我的地图 JavaScript 中,我遵循了先前引用页面中给出的示例:

//Initialize a map instance.
    map = new atlas.Map('myMap', {
        center: [0, 45],
        zoom: 11,
        view: 'Auto',
        //Add your Azure Maps primary subscription key to the map SDK.
        authOptions: {
            authType: 'anonymous',
            clientId: "<My client ID>", //Your Azure Active Directory client id for accessing your Azure Maps account.
            getToken: function (resolve, reject, map) {
                var tokenServiceUrl = "http://localhost:7071/api/TokenService";
                fetch(tokenServiceUrl)
                    .then(
                        function(r) {
                            console.log("Status is " + r.status);
                            r.text().then(function(token) {
                                console.log("Token is " + token);
                                resolve(token);
                            });
                        });
            }
        }
    });

此代码成功调用了令牌服务 API,并获取了一个令牌(触发了 tokenacquired 事件),但是对于每个被调用的地图 URL,我都得到了 403 响应状态代码。我的 AAD 租户中的登录用户具有经典管理员权限,我还明确将该用户添加到 Maps Reader 角色中。所以我不知道这里会发生什么,也不知道为什么我的用户似乎没有访问权限,即使它从服务中获取了有效令牌。有什么想法吗?

Azure Maps REST API 不支持从基于 sign-in 凭据的 MSA 颁发的访问令牌。您必须使用组织帐户或服务主体。

在提供的文档中 link 样本说

clientId: "", // azure map account client id

因此还要确认您将值设置为 Azure Maps 帐户客户端 ID。

还有 Documentation 上的另一个示例:

var map = new atlas.Map("map", {
  center: [-73.985708, 40.75773],
  style: "grayscale_dark",
  zoom: 12,
  view: "Auto",
  //Add your Azure Maps subscription client ID to the map SDK. Get an Azure Maps client ID at https://azure.com/maps
  authOptions: {
    authType: "anonymous",
    clientId: "35267128-0f1e-41de-aa97-f7a7ec8c2dbd",  // <--- this is Azure Maps Client ID, not Azure AD application registration client id.
    getToken: function(resolve, reject, map) {
      //URL to your authentication service that retrieves an Azure Active Directory Token.
      var tokenServiceUrl = "https://adtokens.azurewebsites.net/api/HttpTrigger1?code=dv9Xz4tZQthdufbocOV9RLaaUhQoegXQJSeQQckm6DZyG/1ymppSoQ==";

      fetch(tokenServiceUrl).then(r => r.text()).then(token => resolve(token));
    }
  }
});

另外,为了帮助调试体验,在您的 Web 浏览器中,使用网络工具检查 WWW-Authenticate 响应 header 或响应 body。可能会有有用的错误代码和消息。

您还可以查看以下内容Github issue,以帮助检查令牌并确定您发送的令牌是否不受支持。