Az Command powershell for adding urls in CORS of azure app service

790 Views Asked by At

I am in search of az commands (powershell) to add urls in azure app service while deploying

You can refer the attached image for clarity . enter image description here

Thanks in advance

2

There are 2 best solutions below

2
Allen Wu On

Please use Set-AzResource.

An example for your reference.

$webAppName = ""
$subscriptionId = ""
$tenantId = ""
$resourceGroup = ""

Connect-AzAccount -Tenant $tenantId
Select-AzSubscription -SubscriptionId $subscriptionId

$allowedOrigins = @()
$allowedOrigins += "www.example.com"

$PropertiesObject = @{cors = @{allowedOrigins= $allowedOrigins}}

Set-AzResource -PropertyObject $PropertiesObject -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/config -ResourceName $webAppName/web -ApiVersion 2015-08-01 -Force
1
sam256 On

2023 Update

If you are coming to this answer now, it looks like the settings are now under siteConfig not config. Here's a Powershell snippet that will set some cors values and set cors to support credential passing:

    $ResourceGroupName = 'my-resource-group'
    $AppName = 'myWebServiceApp'
    $AllowedCorsHosts = @("https://somedomain.com","https://some.otherdomain.com")

    $AzResourceParams = @{
        ResourceGroupName = $ResourceGroupName
        ResourceName = $AppName
        ResourceType =  "Microsoft.Web/sites"
    }

    $WebAppResource = Get-AzResource @AzResourceParams

    $WebAppResource.Properties.siteConfig.cors = @{
        allowedOrigins = $AllowedCorsHosts 
        supportCredentials = $true
    }

    $WebAppResource | Set-AzResource -Force