Add SSL bindings Custom domain in dynamically created web app service

I have created azure web app using the REST API. I want to add SSL certificate to my web app. My web app is created dynamically by using azure api. So when the web app created, also want to bind the SSL for each web app. Is there is any option to custom domain SSL bindings using rest api ?.

I am using wildcard SSL.

1 answer

  • answered 2018-10-17 09:14 Joey Cai

    Is there is any option to custom domain SSL bindings using rest api ?.

    YES, you could add an existing SSL binding to an Azure web app using rest api.

    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"
      }
    }
    

    More details, you could refer to below C# codes:

    First, create a Josn.txt in your local machine to store the property you will set:

    {
      "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# code:

    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());
    }
    

    Output: enter image description here

    enter image description here

    For more details, you could refer to this article.

    BTW, as Jayendran said, you also could use C# code not with REST API. You could refer to this 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));
    

    Hope it helps you.