0
votes

I have created azure web app using the REST API. Is there is any option to custom domain mapping using rest api ?.

From the below Link, I have created the new web app service.

https://docs.microsoft.com/en-us/rest/api/appservice/webapps/createorupdate

2
Here is a similar issue with you, you could refer to it.Joey Cai
yes, but it using certificates. I am using token based authentication.Jinesh
so whats the question? yes you can do that4c74356b41

2 Answers

4
votes

As @4c74356b41 provided, you could use Web Apps - Create Or Update Host Name Binding to achieve what you want. I test in my site and it works fine, you could refer to the following steps.

1.Go to the webapp that you have created in portal and add permission to the app registered in Azure AD.

enter image description here

2.Go to your DNS configuration UI for your custom domain and follow that instructions.

enter image description here

3.You could follow the code as below.

Note:The hostNameBindings name here is the whole CNAME in your custom domain DNS zone like joey.example.com

var appId = "xxxxxxxxxxxxxxxxx";
var secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var tenantId = "xxxxxxxxxxxxxxxxxxxxxxxx";
var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
ClientCredential clientCredential = new ClientCredential(appId, secretKey);
var tokenResponse = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result;
var accessToken = tokenResponse.AccessToken;
using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
    var baseUrl = new Uri($"https://management.azure.com/");
    var requestURl = baseUrl +@"subscriptions/xxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxxxxxxxxx/providers/Microsoft.Web/sites/xxxxxxxxxx/hostNameBindings/xxxxxxxxxxxxxx?api-version=2016-08-01";
    string body = "{\"properties\": {\"azureResourceName\": \"joey\"}}";
    var stringContent = new StringContent(body, Encoding.UTF8, "application/json");
    var response = client.PutAsync(requestURl, stringContent).Result;
}

The output:

enter image description here

0
votes

https://docs.microsoft.com/en-us/azure/app-service/scripts/app-service-powershell-configure-custom-domain

you can use that article to configure it.

$fqdn="<Replace with your custom domain name>"
$webappname="mywebapp$(Get-Random)"
$location="West Europe"

# Create a resource group.
New-AzureRmResourceGroup -Name $webappname -Location $location

# Create an App Service plan in Free tier.
New-AzureRmAppServicePlan -Name $webappname -Location $location `
-ResourceGroupName $webappname -Tier Free

# Create a web app.
New-AzureRmWebApp -Name $webappname -Location $location -AppServicePlan $webappname `
-ResourceGroupName $webappname

Write-Host "Configure a CNAME record that maps $fqdn to $webappname.azurewebsites.net"
Read-Host "Press [Enter] key when ready ..."

# Before continuing, go to your DNS configuration UI for your custom domain and follow the 
# instructions at https://aka.ms/appservicecustomdns to configure a CNAME record for the 
# hostname "www" and point it your web app's default domain name.

# Upgrade App Service plan to Shared tier (minimum required by custom domains)
Set-AzureRmAppServicePlan -Name $webappname -ResourceGroupName $webappname `
-Tier Shared

# Add a custom domain name to the web app. 
Set-AzureRmWebApp -Name $webappname -ResourceGroupName $webappname `
-HostNames @($fqdn,"$webappname.azurewebsites.net")