如何通过Management Rest API在Azure Web Apps上设置SSL绑定?

时间:2016-04-24 19:49:07

标签: azure ssl automation hostname azure-management-api

我们正在使用WebSite Management SDK在Azure上构建自动部署。 我们目前设法通过代码将主机名添加到Web应用程序。

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

但是我们无法让HostNameSslStates正常工作。 以类似的方式,我们尝试为此网站添加SSL状态。

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

但是这会导致Azure门户上的SSL绑定。

enter image description here

任何有关如何添加的经验/代码示例的人都会调整此HostName的SSL状态?

1 个答案:

答案 0 :(得分:0)

我希望它会帮助别人。

安装管理包

Install-Package Microsoft.Azure.Management.Fluent

设置AAD,设置密钥和密码

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

设置特定应用的订阅下的访问级别

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