Azure Maps: Authenticate with Microsoft Entra ID
See more Azure Maps Examples
Obtains an application-only token and uses it to geocode an address. Configure tenant_id, application client_id, secret value in client_secret, and the Azure Maps account's maps_client_id in private qa_data/azure/maps-entra.json. All four are nonempty strings. The two client IDs identify different resources.
Register the application in the account's Entra tenant and assign its service principal an appropriate Maps data role at the Maps account scope. Azure Maps Search and Render Data Reader is suitable for the geocoding request shown. Token acquisition alone does not grant Maps access. Allow time for role changes to propagate. Other operations, especially batch requests, need the corresponding data actions.
This confidential-client pattern is for a protected service or internal application that can keep a secret. It does not require a webhook or web server. For public clients, use a suitable delegated flow or backend; on supported Azure hosts consider managed identity. Do not log the token, secret, or response body. Protect the configuration file. Cache tokens with an expiry margin; obtain another using the same grant when needed, because no refresh token is issued.
See client credentials protocol for the form-encoded token request.
Chilkat C# Downloads
bool success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global.UnlockBundle for sample code.
Chilkat.JsonObject config = new Chilkat.JsonObject();
config.Strict = true;
success = config.LoadFile("qa_data/azure/maps-entra.json");
if (success != true) {
Debug.WriteLine("Could not load private Entra configuration.");
return;
}
if (config.JsonTypeOf("tenant_id") != 1) {
Debug.WriteLine("Missing or invalid tenant_id.");
return;
}
if (config.StringOfEquals("tenant_id","",true) == true) {
Debug.WriteLine("A nonempty tenant_id is required.");
return;
}
if (config.JsonTypeOf("client_id") != 1) {
Debug.WriteLine("Missing or invalid client_id.");
return;
}
if (config.StringOfEquals("client_id","",true) == true) {
Debug.WriteLine("A nonempty client_id is required.");
return;
}
if (config.JsonTypeOf("client_secret") != 1) {
Debug.WriteLine("Missing or invalid client_secret.");
return;
}
if (config.StringOfEquals("client_secret","",true) == true) {
Debug.WriteLine("A nonempty client_secret is required.");
return;
}
if (config.JsonTypeOf("maps_client_id") != 1) {
Debug.WriteLine("Missing or invalid maps_client_id.");
return;
}
if (config.StringOfEquals("maps_client_id","",true) == true) {
Debug.WriteLine("A nonempty maps_client_id is required.");
return;
}
Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.FollowRedirects = false;
http.ConnectTimeout = 30;
http.ReadTimeout = 90;
http.Accept = "application/json";
Chilkat.HttpResponse response = new Chilkat.HttpResponse();
Chilkat.JsonObject result = new Chilkat.JsonObject();
result.Strict = true;
Chilkat.JsonObject query = new Chilkat.JsonObject();
// This Http object is used only for the token request until it completes.
// The tenant is a directory ID or verified tenant domain, not common/consumers.
success = http.SetUrlVar("tenant",config.StringOf("tenant_id"));
if (success != true) {
Debug.WriteLine("Could not set tenant.");
return;
}
Chilkat.HttpRequest form = new Chilkat.HttpRequest();
form.HttpVerb = "POST";
form.ContentType = "application/x-www-form-urlencoded";
form.AddParam("grant_type","client_credentials");
form.AddParam("client_id",config.StringOf("client_id"));
form.AddParam("client_secret",config.StringOf("client_secret"));
form.AddParam("scope","https://atlas.microsoft.com/.default");
success = http.HttpReq("https://login.microsoftonline.com/{$tenant}/oauth2/v2.0/token",form,response);
// Http methods return false for HTTP errors as well as transport failures.
if (response.StatusCode == 0) {
Debug.WriteLine("No HTTP response. Inspect diagnostics privately; they can contain credentials.");
return;
}
if (response.StatusCode != 200) {
Debug.WriteLine("HTTP status: " + Convert.ToString(response.StatusCode));
Debug.WriteLine("Error code: " + response.GetHeaderField("x-ms-error-code"));
Debug.WriteLine("Retry-After: " + response.GetHeaderField("Retry-After"));
// Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
// Keep response bodies and detailed diagnostics out of public logs.
return;
}
if (success != true) {
Debug.WriteLine("The HTTP response was incomplete.");
return;
}
success = result.Load(response.BodyStr);
if (success != true) {
Debug.WriteLine("Expected a well-formed JSON response.");
return;
}
if (result.JsonTypeOf("access_token") != 1) {
Debug.WriteLine("Missing or invalid access_token.");
return;
}
if (result.StringOfEquals("access_token","",true) == true) {
Debug.WriteLine("A nonempty access_token is required.");
return;
}
if (result.JsonTypeOf("expires_in") != 2) {
Debug.WriteLine("Missing or invalid expires_in.");
return;
}
if (result.StringOfEquals("token_type","Bearer",false) != true) {
Debug.WriteLine("Expected a Bearer token.");
return;
}
Debug.WriteLine("Token lifetime in seconds: " + result.StringOf("expires_in"));
// Cache the token privately and obtain a new one before its lifetime expires.
// Client credentials do not yield a refresh token: repeat the token request.
http.AuthToken = result.StringOf("access_token");
// This is the MAPS ACCOUNT client ID, not the Entra application client_id.
http.SetRequestHeader("x-ms-client-id",config.StringOf("maps_client_id"));
query.UpdateString("api-version","2026-01-01");
query.UpdateString("query","1 Microsoft Way, Redmond, WA");
query.UpdateInt("top",3);
success = http.HttpParams("GET","https://atlas.microsoft.com/geocode",query,response);
// Http methods return false for HTTP errors as well as transport failures.
if (response.StatusCode == 0) {
Debug.WriteLine("No HTTP response. Inspect diagnostics privately; they can contain credentials.");
return;
}
if (response.StatusCode != 200) {
Debug.WriteLine("HTTP status: " + Convert.ToString(response.StatusCode));
Debug.WriteLine("Error code: " + response.GetHeaderField("x-ms-error-code"));
Debug.WriteLine("Retry-After: " + response.GetHeaderField("Retry-After"));
// Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
// Keep response bodies and detailed diagnostics out of public logs.
return;
}
if (success != true) {
Debug.WriteLine("The HTTP response was incomplete.");
return;
}
success = result.Load(response.BodyStr);
if (success != true) {
Debug.WriteLine("Expected a well-formed JSON response.");
return;
}
Chilkat.JsonArray features = new Chilkat.JsonArray();
Chilkat.JsonObject feature = new Chilkat.JsonObject();
if (result.StringOfEquals("type","FeatureCollection",true) != true) {
Debug.WriteLine("Expected a GeoJSON FeatureCollection.");
return;
}
success = result.ArrayOf2("features",features);
if (success != true) {
Debug.WriteLine("Expected a features array.");
return;
}
if (features.Size == 0) {
Debug.WriteLine("No matching locations.");
return;
}
int i = 0;
while ((i < features.Size)) {
success = features.ObjectAt2(i,feature);
if (success != true) {
Debug.WriteLine("Invalid feature.");
return;
}
if (feature.JsonTypeOf("properties.address.formattedAddress") != 1) {
Debug.WriteLine("Missing or invalid properties.address.formattedAddress.");
return;
}
Debug.WriteLine("Address: " + feature.StringOf("properties.address.formattedAddress"));
if (feature.JsonTypeOf("geometry.coordinates[0]") != 2) {
Debug.WriteLine("Missing or invalid geometry.coordinates[0].");
return;
}
if (feature.JsonTypeOf("geometry.coordinates[1]") != 2) {
Debug.WriteLine("Missing or invalid geometry.coordinates[1].");
return;
}
// GeoJSON coordinates are longitude, latitude.
Debug.WriteLine("Longitude: " + feature.StringOf("geometry.coordinates[0]"));
Debug.WriteLine("Latitude: " + feature.StringOf("geometry.coordinates[1]"));
if (feature.JsonTypeOf("properties.confidence") == 1) {
Debug.WriteLine("Confidence: " + feature.StringOf("properties.confidence"));
}
i = i + 1;
}