使用 .NET SDK 建立 DNS 區域和記錄集

您可以使用 DNS SDK 搭配 .NET DNS 管理程式庫,以自動化作業來建立、刪除或更新 DNS 區域、記錄集和記錄。 這裡提供完整的 Visual Studio 專案。

建立服務主體帳戶

一般而言,以程式設計方式存取 Azure 資源的權限會使用專用帳戶授與,而非您自己的使用者認證。 這些專用帳戶稱為「服務主體」帳戶。 若要使用 Azure DNS SDK 範例專案,您必須先建立服務主體帳戶,並為其指派正確的權限。

  1. 建立服務主體帳戶。 Azure DNS SDK 範例專案會假設使用密碼型驗證。

  2. 然後建立資源群組

  3. 使用 Azure RBAC 授與服務主體帳戶「DNS 區域參與者」權限給資源群組。

  4. 若您正在使用 Azure DNS SDK 範例專案,請按照以下步驟編輯 'program.cs' 檔案:

    • 針對步驟 1 所使用的 tenantIdclientId (也稱為帳戶識別碼)、secret (服務主體帳戶密碼) 與 subscriptionId,插入正確的值。
    • 輸入在步驟 2 中建立的資源群組名稱。
    • 輸入您選擇的 DNS 區域名稱。

NuGet 封裝和命名空間宣告

若要使用 Azure DNS .NET SDK,您需要安裝 Azure DNS 管理程式庫 NuGet 封裝以及其他必要的 Azure 封裝。

  1. Visual Studio中,開啟專案或新專案。

  2. 前往 [工具]>[NuGet 套件管理員]>[管理解決方案的 NuGet 套件...]

  3. 選取 [瀏覽],啟用 [包含發行前版本] 核取方塊,然後在搜尋方塊中鍵入 Microsoft.Azure.Management.Dns

  4. 選取該套件,然後選取 [安裝],將其新增至您的 Visual Studio 專案。

  5. 重複上述程序以便一併安裝下列封裝︰Microsoft.Rest.ClientRuntime.Azure.AuthenticationMicrosoft.Azure.Management.ResourceManager

新增命名空間宣告

新增下列命名空間宣告

using Microsoft.Rest.Azure.Authentication;
using Microsoft.Azure.Management.Dns;
using Microsoft.Azure.Management.Dns.Models;

初始化 DNS 管理用戶端

DnsManagementClient 包含管理 DNS 區域和記錄集所需的方法和屬性。 下列程式碼會登入服務主體帳戶,並建立 DnsManagementClient 物件。

// Build the service credentials and DNS management client
var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, secret);
var dnsClient = new DnsManagementClient(serviceCreds);
dnsClient.SubscriptionId = subscriptionId;

建立或更新 DNS 區域

若要建立 DNS 區域,您必須先建立包含 DNS 區域參數的「區域」物件。 因為 DNS 區域未連結至特定區域,所以位置設定為 'global'。 在此範例中,還會對此區域新增 Azure Resource Manager「標籤」

若要在 Azure DNS 中建立或更新區域,則會將包含區域參數的區域物件傳遞至 DnsManagementClient.Zones.CreateOrUpdateAsyc 方法。

注意

DnsManagementClient 支援三種作業模式︰同步 ('CreateOrUpdate')、非同步 ('CreateOrUpdateAsync'),或非同步並可存取 HTTP 回應 ('CreateOrUpdateWithHttpMessagesAsync')。 您可以根據應用程式的需要選擇上述任何模式。

Azure DNS 支援開放式同步存取,稱為 Etag。 在此範例中,為 'If-None-Match' 標頭指定 "*" 會告訴 Azure DNS 要建立 DNS 區域 (若尚未存在任何區域)。 如果指定的資源群組中已存在具有指定名稱的區域,呼叫就會失敗。

// Create zone parameters
var dnsZoneParams = new Zone("global"); // All DNS zones must have location = "global"

// Create an Azure Resource Manager 'tag'.  This is optional.  You can add multiple tags
dnsZoneParams.Tags = new Dictionary<string, string>();
dnsZoneParams.Tags.Add("dept", "finance");

// Create the actual zone.
// Note: Uses 'If-None-Match *' ETAG check, so will fail if the zone exists already.
// Note: For non-async usage, call dnsClient.Zones.CreateOrUpdate(resourceGroupName, zoneName, dnsZoneParams, null, "*")
// Note: For getting the http response, call dnsClient.Zones.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, zoneName, dnsZoneParams, null, "*")
var dnsZone = await dnsClient.Zones.CreateOrUpdateAsync(resourceGroupName, zoneName, dnsZoneParams, null, "*");

建立 DNS 記錄集和記錄

DNS 記錄是以記錄集形式管理。 記錄集是指一個區域內有相同名稱和記錄類型的一組記錄。 記錄集名稱會相對於區域名稱而非完整的 DNS 名稱。

若要建立或更新記錄集,則會建立「RecordSet」參數物件並傳遞給 DnsManagementClient.RecordSets.CreateOrUpdateAsync。 和 DNS 區域的情況一樣,作業模式也有三種︰同步 ('CreateOrUpdate')、非同步 ('CreateOrUpdateAsync'),或非同步並可存取 HTTP 回應 ('CreateOrUpdateWithHttpMessagesAsync')。

和 DNS 區域的情況一樣,記錄集上的作業包含開放式並行存取支援。 在此範例中,因為 'If-Match' 或 'If-None-Match' 都未指定,所以一律會建立記錄集。 這個呼叫會覆寫此 DNS 區域中任何具有相同名稱和記錄類型的現有記錄集。

// Create record set parameters
var recordSetParams = new RecordSet();
recordSetParams.TTL = 3600;

// Add records to the record set parameter object.  In this case, we'll add a record of type 'A'
recordSetParams.ARecords = new List<ARecord>();
recordSetParams.ARecords.Add(new ARecord("1.2.3.4"));

// Add metadata to the record set.  Similar to Azure Resource Manager tags, this is optional and you can add multiple metadata name/value pairs
recordSetParams.Metadata = new Dictionary<string, string>();
recordSetParams.Metadata.Add("user", "Mary");

// Create the actual record set in Azure DNS
// Note: no ETAG checks specified, will overwrite existing record set if one exists
var recordSet = await dnsClient.RecordSets.CreateOrUpdateAsync(resourceGroupName, zoneName, recordSetName, RecordType.A, recordSetParams);

取得區域和記錄集

DnsManagementClient.Zones.GetDnsManagementClient.RecordSets.Get 方法會分別擷取個別的區域和記錄集。 RecordSets 依其類型、名稱及所在的區域和資源群組來識別。 Zones 依其名稱及所在的資源群組來識別。

var recordSet = dnsClient.RecordSets.Get(resourceGroupName, zoneName, recordSetName, RecordType.A);

更新現有記錄集

若要更新現有的 DNS 記錄集,請先擷取記錄集。 然後,在提交變更之前,先更新記錄集內容。 在此範例中,我們會從 'If-Match' 參數中所擷取的記錄集指定 'Etag'。 如果並行作業在此同時也修改了記錄集,則呼叫會失敗。

var recordSet = dnsClient.RecordSets.Get(resourceGroupName, zoneName, recordSetName, RecordType.A);

// Add a new record to the local object.  Note that records in a record set must be unique/distinct
recordSet.ARecords.Add(new ARecord("5.6.7.8"));

// Update the record set in Azure DNS
// Note: ETAG check specified, update will be rejected if the record set has changed in the meantime
recordSet = await dnsClient.RecordSets.CreateOrUpdateAsync(resourceGroupName, zoneName, recordSetName, RecordType.A, recordSet, recordSet.Etag);

列出區域和記錄集

  • 若要列出區域,請使用DnsManagementClient.Zones.List... 方法,其支援列出指定資源群組中的所有區域,或是指定 Azure 訂用帳戶中的所有區域 (跨資源群組)。

  • 若要列出記錄集,請使用 DnsManagementClient.RecordSets.List... 方法,其支援列出指定區域中的所有記錄集,或只列出特定類型的記錄集。

請注意,在列出區域和記錄集時,其結果可能會分頁。 下列範例示範如何逐一查看結果頁面 (範例中使用了 '2' 的人為小型頁面大小來強制分頁;實際上應該忽略此參數並使用預設頁面大小)。

// Note: in this demo, we'll use a very small page size (2 record sets) to demonstrate paging
// In practice, to improve performance you would use a large page size or just use the system default
int recordSets = 0;
var page = await dnsClient.RecordSets.ListAllInResourceGroupAsync(resourceGroupName, zoneName, "2");
recordSets += page.Count();

while (page.NextPageLink != null)
{
    page = await dnsClient.RecordSets.ListAllInResourceGroupNextAsync(page.NextPageLink);
    recordSets += page.Count();
}

下一步

下載 Azure DNS .NET SDK 範例專案。 包含如何使用 Azure DNS .NET SDK 的範例,以及其他 DNS 記錄類型的範例。