Create Azure Active Directory App Registration with Azure CLI

BEN ABT
medialesson
Published in
3 min readMar 9, 2022
Photo by Peter Olexa on Unsplash

Create Azure Active Directory App Registration with Azure CLI

There are now an insane number of ways to register applications in Azure Active Directory — but many ways are no longer supported or have been discontinued, for example the old Azure PowerShell tools.

However, new tools — for example PowerShell Az — do not support all features, are not well documented or do not behave as expected. In addition, for months there has been a bug in PowerShell that makes the tooling installation take up to 60 minutes! Powershell Gallery slowness: Install-Module -Name Az takes 60 minutes instead of 3

However, one way is still stable and working: the Azure CLI.

Install Azure CLI

Using WinGet:

winget install -e --id Microsoft.AzureCLI

Using Chocolatey:

choco install azure-cli

Manual Install: Azure CLI Docs

Login

Login into your Azure Account from CLI:

or use device code login to use a custom browser window (e.g. multi account feature of your browser):

az login --use-device-code

Select a subscription

az account set --subscription $subscriptionId

Create Azure App Registration

When creating the app, it is important to consider what type of app is desired. By default, certain parameters always refer to a web app, e.g. Reply URLs. If a SPA is desired, an update must also take place after the creation!

Create WebApp

$uri = "https://ba-sample-webapp.azurewebsites.net/" $appName = "Benjamin Abt Sample WebApp"
$appHomepage = "https://ba-sample-webapp.azurewebsites.net/" $appReplyUrls = @("https://ba-sample-webapp.azurewebsites.net/", "https://ba-sample-webapp.azurewebsites.net/logout/")
Write-Host "Web App Creating.."$app = az ad app create --display-name $appName --homepage $appHomepage --reply-urls $appReplyUrls | ConvertFrom-Json Write-Host "Web App $($app.appId) Created."

Create SPA App

$uri = "https://ba-sample-webapp.azurewebsites.net/"
$appName = "Benjamin Abt Sample WebApp"
$appHomepage = "https://ba-sample-webapp.azurewebsites.net/" $appReplyUrls = @("https://ba-sample-webapp.azurewebsites.net/", "https://ba-sample-webapp.azurewebsites.net/logout/")
Write-Host "SPA App Creating.."
$app = az ad app create --display-name $appName --homepage $appHomepage | ConvertFrom-Json
Write-Host "SPA App
$($app.appId) Created."
Write-Host "SPA App Updating.."
# there is no CLI support to add reply urls to a SPA, so we have to patch manually via az rest
$appPatchUri = "https://graph.microsoft.com/v1.0/applications/{0}" -f $app.objectId $appReplyUrlsString = "'{0}'" -f ($appReplyUrls -join "','")
$appPatchBody = "{spa:{redirectUris:[$appReplyUrlsString]}}"
az rest --method PATCH --uri $appPatchUri --headers 'Content-Type=application/json' --body $appPatchBody
Write-Host "SPA App Updated."

Conclusion:

It is still very simple and fast to create Azure AD App Registrations, however it is just not well documented.

Autor

Benjamin Abt

Ben is a passionate developer and software architect and especially focused on .NET, cloud and IoT. In his professional he works on high-scalable platforms for IoT and Industry 4.0 focused on the next generation of connected industry based on Azure and .NET. He runs the largest german-speaking C# forum myCSharp.de, is the founder of the Azure UserGroup Stuttgart, a co-organizer of the AzureSaturday, runs his blog, participates in open source projects, speaks at various conferences and user groups and also has a bit free time. He is a Microsoft MVP since 2015 for .NET and Azure.

Originally published at https://schwabencode.com on March 9, 2022.

--

--