diff --git a/data-explorer/kusto/api/rest/authenticate-with-msal.md b/data-explorer/kusto/api/rest/authenticate-with-msal.md index 78b7598d05..b7767e60db 100644 --- a/data-explorer/kusto/api/rest/authenticate-with-msal.md +++ b/data-explorer/kusto/api/rest/authenticate-with-msal.md @@ -72,7 +72,7 @@ request.Headers.Set(HttpRequestHeader.Authorization, string.Format(CultureInfo.I ## Perform application authentication with MSAL -The following code sample shows how to use MSAL to get an authorization token for your cluster. In this flow, no prompt is presented. The application must be registered with Microsoft Entra ID and have an app key or an X509v2 certificate issued by Microsoft Entra ID. To set up an application, see [Provision a Microsoft Entra application](../../access-control/provision-entra-id-app.md). +The following code sample shows how to use MSAL to get an authorization token for your cluster. In this flow, no prompt is presented. The application must be registered with Microsoft Entra ID and have an app key or an X509v2 certificate issued by Microsoft Entra ID. To set up an application, see [Provision a Microsoft Entra application](../../access-control/provision-entra-id-app.md). This is supported for the "ingest-" endpoint as well. ```csharp var kustoUri = "https://..kusto.windows.net"; @@ -82,8 +82,23 @@ var authClient = ConfidentialClientApplicationBuilder.Create("") .WithClientSecret("") // Can be replaced by .WithCertificate to authenticate with an X.509 certificate .Build(); + + string resourceId = null; + + try + { + // Get the appropiate resource id by querying the metadata + var httpClient = new HttpClient(); + var response = httpClient.GetByteArrayAsync($"{kustoUri}/v1/rest/auth/metadata").Result; + var json = JObject.Parse(Encoding.UTF8.GetString(response)); + resourceId = json["AzureAD"]?["KustoServiceResourceId"]?.ToString(); +} +catch { + resourceId = "https://kusto.kusto.windows.net"; +} + var result = authClient.AcquireTokenForClient( - new[] { $"{kustoUri}/.default" } // Define scopes for accessing Azure Data Explorer cluster + new[] { $"{resourceId}/.default" } // Define scopes for accessing Azure Data Explorer cluster ).ExecuteAsync().Result; var bearerToken = result.AccessToken;