本文介绍了在动态创建的 Web 应用服务中添加自定义域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 REST API 创建了 azure Web 应用程序.是否有任何选项可以使用 rest api 自定义域映射?

通过下面的链接,我创建了新的网络应用服务.

2.转到自定义域的 DNS 配置 UI,然后按照

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;使用 (var client = new HttpClient()){client.DefaultRequestHeaders.Add("Authorization", "Bearer" + accessToken);var baseUrl = new Uri($"https://management.azure.com/");var requestURl = baseUrl +@"subscriptions/xxxxxxxxxxxxxxxxxx/resourceGroups/xxxxxxxxxxxx/providers/Microsoft.Web/sites/xxxxxxxxxx/hostNameBindings/xxxxxxxxxxxxxx?api-version=2016-08-01";字符串正文 = "{"properties": {"azureResourceName":"joey"}}";var stringContent = new StringContent(body, Encoding.UTF8, "application/json");var response = client.PutAsync(requestURl, stringContent).Result;}

输出:

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

解决方案

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.

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

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:

这篇关于在动态创建的 Web 应用服务中添加自定义域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 22:21