2

We are building automated deployment on Azure using the WebSite Management SDK. We currently managed to add hostnames to a Web App via code.

using (websiteClient)
{
     var configuration = websiteClient.WebSites.Get(webspace, WebsiteName + "-" + Version, new WebSiteGetParameters());

     configuration.WebSite.HostNames.Add(ClientName + "." + DnsZoneName);

     var response = websiteClient.WebSites.Update(webspace, WebsiteName + "-" + Version, new WebSiteUpdateParameters() { HostNames = configuration.WebSite.HostNames });
}

But we cant get the HostNameSslStates to work properly. In a simular fashion we try to Add the SSL State for this website.

 configuration.WebSite.HostNameSslStates.Add(new WebSite.WebSiteHostNameSslState() { Name = ClientName + "." + DnsZoneName, SslState = WebSiteSslState.SniEnabled, Thumbprint = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" });

But this doent result in a SSL binding on the Azure portal.

enter image description here

Anybody who has any experience / Code samples on how to add adjust the SSL State of this HostName?

Anthony Claeys
  • 231
  • 1
  • 9

1 Answers1

3

I hope it will help someone.

Install Management Package

Install-Package Microsoft.Azure.Management.Fluent

Setup AAD, Setup Key and Password

https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal#get-application-id-and-authentication-key

Setup Access level under subscription for specific app

https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal

var creds = new AzureCredentialsFactory().FromServicePrincipal( APPLICATION_ID, KEY_PASSWORD, DIRECTORY_ID, AzureEnvironment.AzureGlobalCloud);
var azure = Azure.Authenticate(creds).WithSubscription(SUSCRIPTION_ID);
var apps = azure.WebApps.List( );
foreach( var app in apps )
{
    if( app.Name == REQUIRED_APP_NAME  )
    {
        Console.WriteLine( app.Name );
        Console.WriteLine( $"Applying domain name {SUB_DOMAIN}.{DOMAIN}" );

        var updatedApp = app.Update( )
                                      .DefineHostnameBinding( )
                                      .WithThirdPartyDomain( DOMAIN )
                                      .WithSubDomain( SUB_DOMAIN )
                                      .WithDnsRecordType( CustomHostNameDnsRecordType.CName )
                                      .Attach( )
                                      .Apply( );

         Console.WriteLine( $"Applying SSL for {SUB_DOMAIN}.{DOMAIN}" );

         await azure
                    .WebApps
                    .Inner
                    .CreateOrUpdateHostNameBindingWithHttpMessagesAsync(       
                        RESOURCE_GROUP,
                        app.Name,
                        $"{SUB_DOMAIN}.{DOMAIN}",
                        new HostNameBindingInner( 
                            azureResourceType: AzureResourceType.Website,
                            hostNameType: HostNameType.Verified,
                            customHostNameDnsRecordType: CustomHostNameDnsRecordType.CName,
                            sslState: SslState.SniEnabled,
                            thumbprint: EXISTING_THUMBPRINT ) );

         Console.WriteLine( "OK" );
    }
}
Maxim
  • 144
  • 1
  • 9