问题
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