I want to automate the creation of custom domains
The AddCname and AddTxt Methods work correctly
Add Subdomain gives error 400
I am a bit lost on how to create the hostnamebinding. What values do i really need and how can i get them from azure (DomainId, ThumbprintString)?
How do i get the verification code after creating the hostnamebinding for my TXT-Record?
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);
// Add CNAME record
await AddCNameRecordAsync("pit-services.one", subdomainName, $"{appServiceName}.azurewebsites");
// Add TXT record for domain verification
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);
}
}
It works now this is my solution.
I want to automate the creation of custom domains
The AddCname and AddTxt Methods work correctly
Add Subdomain gives error 400
I am a bit lost on how to create the hostnamebinding. What values do i really need and how can i get them from azure (DomainId, ThumbprintString)?
How do i get the verification code after creating the hostnamebinding for my TXT-Record?
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);
// Add CNAME record
await AddCNameRecordAsync("pit-services.one", subdomainName, $"{appServiceName}.azurewebsites");
// Add TXT record for domain verification
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);
}
}
It works now this is my solution.
Share Improve this question edited Mar 13 at 15:09 user27378175 asked Mar 13 at 11:19 user27378175user27378175 113 bronze badges 01 Answer
Reset to default 0The code below fetches the necessary Azure resources, including the Resource Group, App Service, and DNS Zone. It also adds a subdomain with hostname binding, creates the required CNAME and TXT records for domain verification, and handles any missing verification records.
using System;
using System.Linq;
using System.Threading.Tasks;
using Azure;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.AppService;
using Azure.ResourceManager.AppService.Models;
using Azure.ResourceManager.Dns;
using Azure.ResourceManager.Dns.Models;
using Azure.ResourceManager.Resources;
public class AzureService
{
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, string domainName)
{
var appService = await GetAppServiceAsync(appServiceName);
string subdomainName = subdomain.Replace($".{domainName}", "");
string txtRecordName = $"asuid.{subdomainName}";
// Fetch the verification ID from App Service
string verificationId = appService.Data.CustomDomainVerificationId;
// Create TXT Record for domain verification
await AddTxtRecordAsync(domainName, txtRecordName, verificationId);
// Create the hostname binding
var hostNameBindingData = new HostNameBindingData
{
SslState = HostNameBindingSslState.SniEnabled,
HostNameType = AppServiceHostNameType.Verified, // Fixed type
AzureResourceType = AppServiceResourceType.Website // Fixed type
};
await appService.GetSiteHostNameBindings().CreateOrUpdateAsync(WaitUntil.Completed, subdomain, hostNameBindingData);
// Add CNAME Record pointing to App Service
await AddCNameRecordAsync(domainName, subdomainName, $"{appServiceName}.azurewebsites");
}
public async Task AddCNameRecordAsync(string dnsZoneName, string recordSetName, string alias)
{
var dnsZone = await GetDnsZoneAsync(dnsZoneName);
var cnameRecordSetData = new DnsCnameRecordData
{
TtlInSeconds = 3600,
Cname = alias
};
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 } } }
};
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);
}
}