如何通过Management Rest API在Azure Web Apps上设置SSL绑定? [英] How to set SSL bindings on Azure Web Apps via Management Rest API?

查看:59
本文介绍了如何通过Management Rest API在Azure Web Apps上设置SSL绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

但这确实会导致Azure门户上的SSL绑定.

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

任何有经验/有关于如何添加代码示例的人都可以调整此主机名的SSL状态?

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

推荐答案

我希望它会对某人有所帮助.

I hope it will help someone.

安装管理包

Install-Package Microsoft.Azure.Management.Fluent

设置AAD,设置密钥和密码

Setup AAD, Setup Key and Password

https://docs.microsoft.com/zh-cn/azure/azure-resource-manager/resource-group-create-service-principal-portal#get-application-id和认证密钥

针对特定应用的订阅下的设置访问权限级别

Setup Access level under subscription for specific app

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

这篇关于如何通过Management Rest API在Azure Web Apps上设置SSL绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆