首页 > 解决方案 > 为什么我的 Azure 地图控件的匿名身份验证不起作用?

问题描述

我正在开发一个单页应用程序,并尝试设置此处详述的匿名身份验证。我有一个本地运行的 Azure 函数来调用令牌服务提供程序以获取令牌:

 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 在本地调用函数端点会返回 200 状态和一个令牌,所以这看起来很不错。

在我的 Maps 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-active-directoryazure-maps

解决方案


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

在提供的文档链接上,示例说

clientId: "", // 天蓝色地图账号客户端id

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

另一个文档示例:

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响应标头或响应正文。可能会有有用的错误代码和消息。

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


推荐阅读