floatingfrisbee
floatingfrisbee

Reputation: 938

How to make an Azure app registration with platform SPA via Powershell

We use PowerShell to set up an Azure deployment, which, among other Azure resources, creates an app registration.

The simplified code is as follows:

$appRegistration = New-AzADApplication `
    -DisplayName $applicationName `
    -HomePage "$webAppUrl" `
    -IdentifierUris "api://$webAppName";

To it, we add redirect uris, like this:

if ($redirectUris -notcontains "$webAppUrl") {
    $redirectUris.Add("$webAppUrl");    
    Write-Host "Adding $webAppUrl to redirect URIs";
}

if ($redirectUris -notcontains "$webAppUrl/aad-auth") {
    $redirectUris.Add("$webAppUrl/aad-auth");
    Write-Host "Adding $webAppUrl/aad-auth to redirect URIs";
}

Update-AzADApplication `
    -ApplicationId $applicationId `
    -IdentifierUris "api://$applicationId" `
    -ReplyUrl $redirectUris | Out-Null

This works great, and an app registration with the "web" platform is created. It looks like this:

App registration showing Web platform

My question is how can we get these redirect uris to be under the "SPA" platform, using PowerShell? Like in the image below, which was done manually on the Portal.

App registration showing SPA platform

Upvotes: 4

Views: 2796

Answers (2)

Joy Wang
Joy Wang

Reputation: 42043

Looks there is no feature in the built-in command to do that, you could call the MS Graph - Update application in the powershell directly.

You could refer to the sample below work for me, make sure your service principal/user acount logged in Az via Connect-AzAccount has the permission to call the API.

$objectId = "xxxxxxxxxxxxxxxx"
$redirectUris = @()
$webAppUrl = "https://joyweb.azurewebsites.net"
if ($redirectUris -notcontains "$webAppUrl") {
    $redirectUris += "$webAppUrl"   
    Write-Host "Adding $webAppUrl to redirect URIs";
}

if ($redirectUris -notcontains "$webAppUrl/aad-auth") {
    $redirectUris += "$webAppUrl/aad-auth"
    Write-Host "Adding $webAppUrl/aad-auth to redirect URIs";
}

$accesstoken = (Get-AzAccessToken -Resource "https://graph.microsoft.com/").Token
$header = @{
    'Content-Type' = 'application/json'
    'Authorization' = 'Bearer ' + $accesstoken
}
$body = @{
    'spa' = @{
        'redirectUris' = $redirectUris
    }
} | ConvertTo-Json

Invoke-RestMethod -Method Patch -Uri "https://graph.microsoft.com/v1.0/applications/$objectId" -Headers $header -Body $body

Check the result in the portal:

enter image description here

Upvotes: 3

Marilee Turscak - MSFT
Marilee Turscak - MSFT

Reputation: 7720

There was a similar thread where someone was trying to programmatically add the redirect URIs for SPA and could not do it because it defaults under the Web section.

He was able to resolve this by posting with Azure CLI to the Graph API:

az rest `
    --method PATCH `
    --uri 'https://graph.microsoft.com/v1.0/applications/{id}' `
    --headers 'Content-Type=application/json' `
    --body "{spa:{redirectUris:['http://localhost:3000']}}"

Upvotes: 4

Related Questions