You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Azure SDK ResourceManager配置自定义域名的问题及解决方案

Azure SDK ResourceManager 自定义域名自动化创建问题与解决方案

我尝试通过Azure SDK的ResourceManager模块自动化创建自定义域名,目前AddCname和AddTxt方法可正常运行,但添加子域名时出现400错误。同时有以下疑问:

  • 如何创建HostnameBinding?需要哪些参数(如DomainId、ThumbprintString),以及如何从Azure获取这些值?
  • 创建HostnameBinding后,如何获取用于TXT记录的验证代码?

已解决的实现代码

public class AzureService : IAzureService
{
    private readonly ArmClient _client;
    private readonly SubscriptionResource _subscription;
    private readonly ResourceGroupResource _resourceGroup;

    public AzureService(string resourceGroupName)
    {
        _client = new ArmClient(new DefaultAzureCredential());
        _subscription = _client.GetDefaultSubscription();
        _resourceGroup = _subscription.GetResourceGroup(resourceGroupName);
    }

    public async Task AddSubdomainAsync(string appServiceName, string subdomain)
    {
        var appService = await GetAppServiceAsync(appServiceName);

        var subdomainParts = subdomain.Split('.');
        var subdomainName = string.Join('.', subdomainParts.Take(subdomainParts.Length - 2));
        var txtRecordName = $"asuid.{subdomainName}";

        var hostNameBindingData = new HostNameBindingData
        {
            SslState = HostNameBindingSslState.SniEnabled
        };

        var operation = await appService.GetSiteHostNameBindings().CreateOrUpdateAsync(WaitUntil.Completed, subdomain, hostNameBindingData);

        // 添加CNAME记录
        await AddCNameRecordAsync("pit-services.one", subdomainName, $"{appServiceName}.azurewebsites.net");

        // 添加用于域名验证的TXT记录
        await AddTxtRecordAsync("pit-services.one", txtRecordName, appService.Data.CustomDomainVerificationId);
    }

    public async Task AddCNameRecordAsync(string dnsZoneName, string recordSetName, string alias)
    {
        var dnsZone = await GetDnsZoneAsync(dnsZoneName);
        var cnameRecordSetData = new DnsCnameRecordData
        {
            TtlInSeconds = 3600,
            Cname = alias
        };

        var cnameRecordSet = await dnsZone.GetDnsCnameRecords().CreateOrUpdateAsync(WaitUntil.Completed, recordSetName, cnameRecordSetData);
    }

    public async Task AddTxtRecordAsync(string dnsZoneName, string recordSetName, string value)
    {
        var dnsZone = await GetDnsZoneAsync(dnsZoneName);
        var txtRecordSetData = new DnsTxtRecordData
        {
            TtlInSeconds = 3600,
            DnsTxtRecords = { new DnsTxtRecordInfo { Values = { value } } }
        };

        var txtRecordSet = await dnsZone.GetDnsTxtRecords().CreateOrUpdateAsync(WaitUntil.Completed, recordSetName, txtRecordSetData);
    }

    private async Task<DnsZoneResource> GetDnsZoneAsync(string dnsZoneName)
    {
        return await _resourceGroup.GetDnsZoneAsync(dnsZoneName);
    }

    public async Task<WebSiteResource> GetAppServiceAsync(string appServiceName)
    {
        return await _resourceGroup.GetWebSiteAsync(appServiceName);
    }
}

关键说明

  1. HostnameBinding创建:无需额外的DomainId或ThumbprintString参数,仅需指定完整格式的子域名(如sub.pit-services.one)和SSL状态(示例中启用SNI),通过WebSiteResource.GetSiteHostNameBindings().CreateOrUpdateAsync方法完成创建。
  2. 验证代码获取:直接从appService.Data.CustomDomainVerificationId获取该值,它是App Service实例的全局验证ID,用于生成TXT记录完成域名所有权验证。

内容的提问来源于stack exchange,提问作者user27378175

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 23:17:11