微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我正在开发一个单页应用程序,并尝试按照 here 的详细信息设置匿名身份验证。我有一个在本地运行的 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,并获取令牌(令牌获取事件触发),但我为每个被调用的地图 URL 得到 403 响应状态代码。我的 AAD 租户中的登录用户具有经典管理员权限,我还明确地将该用户添加到地图阅读器角色中。所以我不知道这里可能会发生什么,以及为什么我的用户似乎没有访问权限,即使它从服务中获取一个有效的令牌。这里有什么想法吗?

解决方法

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

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

clientId: "",// azure map 帐号客户端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));
    }
  }
});

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。