Azure WebApp添加带绑定自定义域名:旧SDK适配及新SDK方案咨询
Azure WebApp 添加带SSL绑定的自定义域名方案
旧Microsoft.Azure.Management.Fluent SDK实现SSL绑定
你当前的代码仅完成了主机名绑定的添加,未配置SSL证书关联,因此域名状态显示未加密。该旧SDK支持添加SSL绑定,只需在Update链中补充SSL绑定的定义:
var webapp = GetWebApp(request.AzureHostName); if(webapp is null) { throw new Exception("Failed to get webapp for adding custom domain, will not be done"); } // 拼接完整自定义域名 var fullDomain = $"{request.SubDomainName}.{request.DomainName}"; webapp.App.Update() // 添加主机名绑定 .DefineHostnameBinding() .WithThirdPartyDomain(request.DomainName) .WithSubDomain(request.SubDomainName) .WithDnsRecordType(CustomHostNameDnsRecordType.CName) .Attach() // 添加SSL绑定(关联已上传至App Service的证书) .DefineSslBinding() .ForHostname(fullDomain) // 通过证书指纹指定已上传的证书 .WithExistingCertificate("<Certificate-Thumbprint>") // 使用SNI型SSL(支持多域名共用IP) .WithSniBasedSsl() .Attach() .Apply();
注意:使用前需确保证书已上传至当前WebApp所在App Service计划的证书库,且证书覆盖该自定义域名。
新Azure.ResourceManager SDK实现方案
推荐迁移至Azure.ResourceManager.AppService SDK(NuGet包),以下是完整实现步骤:
1. 初始化ArmClient
通过Azure服务凭据初始化资源客户端:
using Azure.Identity; using Azure.ResourceManager; using Azure.ResourceManager.AppService; using Azure.ResourceManager.AppService.Models; var credential = new ClientSecretCredential( tenantId: "<Your-Tenant-Id>", clientId: "<Your-Client-Id>", clientSecret: "<Your-Client-Secret>" ); var armClient = new ArmClient(credential);
2. 获取目标WebApp资源
var subscriptionId = "<Your-Subscription-Id>"; var resourceGroupName = "<Your-Resource-Group-Name>"; var webAppName = request.AzureHostName; var webAppResourceId = WebAppResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, webAppName); var webApp = armClient.GetWebAppResource(webAppResourceId); if (!webApp.Exists()) { throw new Exception("Failed to get webapp for adding custom domain, will not be done"); }
3. 添加主机名绑定
var fullDomain = $"{request.SubDomainName}.{request.DomainName}"; var hostnameBindingData = new HostNameBindingData() { HostNameType = HostNameBindingType.Verified, SiteName = webAppName, CustomHostNameDnsRecordType = CustomHostNameDnsRecordType.CName, HostName = fullDomain }; // 创建主机名绑定(自动完成域名验证) await webApp.GetHostNameBindings().CreateOrUpdateAsync(WaitUntil.Completed, fullDomain, hostnameBindingData);
4. 添加SSL绑定
关联已上传的证书完成加密绑定:
// 方式1:使用已上传的自定义证书(通过证书资源ID) var certificateResourceId = "<Your-Certificate-Resource-Id>"; var sslBindingData = new SslBindingData() { HostName = fullDomain, SslState = SslState.SniEnabled, CertificateId = certificateResourceId }; // 方式2:使用App Service托管证书(需先创建托管证书) // var managedCertData = new AppServiceCertificateData() { HostNames = { fullDomain } }; // var managedCertificate = await webApp.GetAppServiceCertificates().CreateOrUpdateAsync(WaitUntil.Completed, "<Cert-Name>", managedCertData); // var sslBindingData = new SslBindingData() // { // HostName = fullDomain, // SslState = SslState.SniEnabled, // CertificateId = managedCertificate.Id // }; // 创建SSL绑定 await webApp.GetSslBindings().CreateOrUpdateAsync(WaitUntil.Completed, fullDomain, sslBindingData);
注意:第三方域名需先完成DNS验证,主机名绑定创建过程中Azure会自动检测验证记录;推荐使用
SslState.SniEnabled类型,支持多域名共用同一IP。
内容的提问来源于stack exchange,提问作者obgire
相关产品推荐
相关产品推荐

