【问题标题】:Add SSL bindings Custom domain in dynamically created web app service在动态创建的 Web 应用服务中添加 SSL 绑定自定义域
【发布时间】:2018-10-16 12:03:53
【问题描述】:

我使用 REST API 创建了 azure Web 应用程序。我想将 SSL 证书添加到我的 Web 应用程序。我的 Web 应用程序是使用 azure api 动态创建的。所以当web app创建的时候,也要为每个web app绑定SSL。是否有任何选项可以使用 rest api 自定义域 SSL 绑定?

我正在使用通配符 SSL。

【问题讨论】:

  • 请详细说明您的要求以及您当前的所有研究结果
  • 已更新......
  • @Jinesh 你能看看here

标签: azure ssl-certificate azure-web-app-service


【解决方案1】:

是否有任何选项可以使用 rest api 自定义域 SSL 绑定?

,您可以使用 rest api 将现有 SSL 绑定添加到 Azure Web 应用程序。

Url: https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/sites/{snapshotName}?api-version={api-version}

Method: PUT

Parameter:
subscriptionId  The identifier of your subscription where the snapshot is being created.
resourceGroup   The name of the resource group that will contain the snapshot.
WebappName    The name of the WebappName. 
api-version The version of the API to use.

Request content:
{
  "properties": {
    "HostNameSslStates": [ 
      {
        "SslState": "the SSL state",
        "ToUpdate": "True",
       "Thumbprint": "The Thumbprint of the certificate, you could find it in the portal",
        "Name": "yourwebsitename"
      }
    ]
},
  "kind": "app",
  "location": "yourlocation",
  "tags": {
    "hidden-related:/subscriptions/{subscriptionId}/resourcegroups/{resourceGroup}/providers/Microsoft.Web/serverfarms/{yourserviceplan}": "empty"
  }
}

更多细节,你可以参考下面的C#代码:

首先,在你的本地机器上创建一个Josn.txt 来存储你要设置的属性:

{
  "properties": {
    "HostNameSslStates": [ 
      {
        "SslState": "1",
        "ToUpdate": "True",
        "Thumbprint": "BE58B05C5CADE03628D0D58B369D0DA6F535B0FA",
        "Name": "example.com"  //your custom domain
      }
    ]
},
  "kind": "app",
  "location": "East Asia",
  "tags": {
    "hidden-related:/subscriptions/xxxxxxxxxxxxxxxx/resourcegroups/xxxxxxxxxxxxx/providers/Microsoft.Web/serverfarms/BrandoTestServicePlan": "empty"
  }
}

C#代码:

string body = File.ReadAllText(@"D:\json.txt");
// Display the file contents to the console. Variable text is a string.
string tenantId = "xxxxxxxxxxxxxxxxxxxxxxxxx";
string clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
string clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxx";
string subscriptionid = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
string resourcegroup = "xxxx";
string appname = "Yourwebapp";
string version = "2018-02-01";

string authContextURL = "https://login.windows.net/" + tenantId;
var authenticationContext = new AuthenticationContext(authContextURL);
var credential = new ClientCredential(clientId, clientSecret);
var result = authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential).Result;

if (result == null)
{
    throw new InvalidOperationException("Failed to obtain the JWT token");
}
string token = result.AccessToken;

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Web/sites/{2}?api-version={3}", subscriptionid, resourcegroup, appname, version));
request.Method = "PUT";
request.Headers["Authorization"] = "Bearer " + token;
request.ContentType = "application/json";
try
{
    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
    {
        streamWriter.Write(body);
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
// Get the response
var httpResponse = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    Console.WriteLine(streamReader.ReadToEnd());
}

输出:

更多详情可以参考这个article

顺便说一句,正如 Jayendran 所说,您也可以使用 C# 代码而不是 REST API。你可以参考这个issue

await azure
        .WebApps
        .Inner
        .CreateOrUpdateHostNameBindingWithHttpMessagesAsync(
            resourceGroupName, 
            webAppName, 
            domain,
            new HostNameBindingInner(
                azureResourceType: AzureResourceType.Website,
                hostNameType: HostNameType.Verified,
                customHostNameDnsRecordType: CustomHostNameDnsRecordType.CName,
                sslState: SslState.SniEnabled,
                thumbprint: thumbprint));

希望对你有帮助。

【讨论】:

  • 谢谢,但我收到错误 'Bad request' 。 'appname' 是指完整地址还是只是名称? abc.azurewerbsites.com 是我的网络应用程序。那么什么是appname?
  • 使用abc就可以了。
  • 好的,那么 "Name": "test.azureclubs.com" 你能告诉我这些 Name 值是什么吗?
  • 没有。您的自定义域名。喜欢example.com
  • 位置是 webapp 的。并且没有任何许可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-27
  • 2016-01-23
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多