How to set SSL bindings on Azure Web Apps via Management Rest API?

非 Y 不嫁゛ 提交于 2019-12-13 02:19:04

问题


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.

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


回答1:


I hope it will help someone.

Install Management Package

Install-Package Microsoft.Azure.Management.Fluent

Setup AAD, Setup Key and Password

https://docs.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://docs.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" );
    }
}


来源:https://stackoverflow.com/questions/36828116/how-to-set-ssl-bindings-on-azure-web-apps-via-management-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!