在动态创建的Web应用程序服务中添加自定义域

时间:2018-08-27 11:06:21

标签: azure azure-web-app-service

我已经使用REST API创建了一个Azure Web应用程序。可以使用rest api进行自定义域映射吗?

通过下面的链接,我创建了新的Web应用程序服务。

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

2 个答案:

答案 0 :(得分:3)

如@ 4c74356b41所提供的,您可以使用Web Apps - Create Or Update Host Name Binding来实现所需的功能。我在我的网站上进行了测试,并且工作正常,您可以参考以下步骤。

1。转到您在门户网站中创建的Web应用程序,然后向在Azure AD中注册的应用程序添加权限

enter image description here

2。转到您的自定义域的DNS配置UI,然后遵循instructions

enter image description here

3。您可以按照以下代码进行操作。

注意:此处的hostNameBindings名称是自定义域DNS区域中的整个CNAME,例如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;
}

输出:

enter image description here

答案 1 :(得分:0)

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

您可以使用该文章对其进行配置。

$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")