Skip to content

Commit 9ebf04b

Browse files
committed
use az rest --method GET instead of az ad app list
1 parent aac9426 commit 9ebf04b

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/dotnet-scaffolding/dotnet-scaffold/AspNet/Helpers/AzCliHelper.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -222,30 +222,29 @@ private static bool GetAzureAppIds(AzCliRunner runner, out List<string> appIds,
222222
{
223223
failingCommand = null;
224224
appIds = [];
225-
var exitCode = runner.RunAzCli("ad app list --output json", out string? stdOut, out string? stdErr);
225+
// Use Microsoft Graph API via az rest
226+
var exitCode = runner.RunAzCli("rest --method GET --url https://graph.microsoft.com/v1.0/applications?$select=appId,displayName", out string? stdOut, out string? stdErr);
226227

227228
if (exitCode == 0 && !string.IsNullOrEmpty(stdOut))
228229
{
229-
// Parse the JSON output
230230
using JsonDocument doc = JsonDocument.Parse(stdOut);
231231
JsonElement root = doc.RootElement;
232-
233-
if (root.ValueKind == JsonValueKind.Array)
232+
if (root.TryGetProperty("value", out JsonElement valueArray) && valueArray.ValueKind == JsonValueKind.Array)
234233
{
235-
236-
foreach (JsonElement app in root.EnumerateArray())
234+
foreach (JsonElement app in valueArray.EnumerateArray())
237235
{
238-
if (app.TryGetProperty("appId", out JsonElement appId))
236+
string? id = app.TryGetProperty("appId", out JsonElement appId) ? appId.GetString() : null;
237+
string? displayName = app.TryGetProperty("displayName", out JsonElement name) ? name.GetString() : "Unknown App";
238+
if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(displayName))
239239
{
240-
string? id = appId.GetString();
241-
string? displayName = app.TryGetProperty("displayName", out JsonElement name) ?
242-
name.GetString() : "Unknown App";
243-
244-
if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(displayName))
240+
// the tool will throw if displayName contains []
241+
if (displayName.Contains("[") || displayName.Contains("]"))
245242
{
246-
// Format as "DisplayName (AppId)" for better user experience
247-
appIds.Add($"{displayName} {id}");
243+
displayName = displayName.Replace("[", "(").Replace("]", ")");
248244
}
245+
246+
// Format as "DisplayName (AppId)" for better user experience
247+
appIds.Add($"{displayName} ({id})");
249248
}
250249
}
251250
return true;
@@ -254,7 +253,7 @@ private static bool GetAzureAppIds(AzCliRunner runner, out List<string> appIds,
254253

255254
if (!string.IsNullOrEmpty(stdErr))
256255
{
257-
failingCommand = $"az ad app list";
256+
failingCommand = "az rest";
258257
}
259258
}
260259
catch (Exception ex)

0 commit comments

Comments
 (0)