快速入門:適用於 .NET 的 Azure 機密總賬客戶端連結庫
開始使用適用於 .NET 的 Azure 機密總賬客戶端連結庫。 Azure 機密總賬 是管理敏感數據記錄的新且高度安全的服務。 根據許可權的區塊鏈模型,Azure 機密總帳提供獨特的資料完整性優勢。 其中包括不變性、只附加總賬和竄改,以確保所有記錄都保持不變。
在本快速入門中,您將瞭解如何使用 .NET 用戶端連結庫在 Azure 機密總帳中建立專案
Azure 機密總賬用戶端連結庫資源:
API 參考檔案 | 連結庫原始碼 | 套件 (NuGet)
必要條件
- Azure 訂用帳戶 - 免費建立一個訂用帳戶
- .NET Core 3.1 SDK 或更新版本
- Azure CLI
您也需要執行中的機密總帳,以及具有許可權的 Administrator
已註冊使用者。 您可以使用 Azure 入口網站、Azure CLI 或 Azure PowerShell 來建立機密總帳(以及系統管理員)。
設定
建立新的 .NET 主控台應用程式
在命令殼層中,執行下列命令以建立名為
acl-app
的專案:dotnet new console --name acl-app
變更為新建立 的 acl-app 目錄,然後執行下列命令來建置專案:
dotnet build
組建輸出不應包含警告或錯誤。
Build succeeded. 0 Warning(s) 0 Error(s)
Install the package
使用 [NuGet][client_nuget_package]安裝適用於 .NET 的機密總賬用戶端連結庫:
dotnet add package Azure.Security.ConfidentialLedger --version 1.0.0
在本快速入門中,您也需要安裝適用於 Azure 身分識別的 Azure SDK 用戶端連結庫:
dotnet add package Azure.Identity
物件模型
適用於 .NET 的 Azure 機密總賬客戶端連結庫可讓您在服務中建立不可變的總賬專案。 [程序代碼範例] 區段示範如何建立寫入總帳,並擷取交易標識碼。
程式碼範例
新增指示詞
將下列指示詞新增至Program.cs頂端:
using System;
using Azure.Core;
using Azure.Identity;
using Azure.Security.ConfidentialLedger;
using Azure.Security.ConfidentialLedger.Certificate;
驗證並建立用戶端
在本快速入門中,登入的用戶可用來向 Azure 機密總帳進行驗證,這是本機開發的慣用方法。 機密總賬的名稱會擴充至密鑰保存庫 URI,格式為 “https://< our-confidential-ledger-name.confidential-ledger.azure.com>”。 此範例使用 Azure 身分識別連結庫的 『DefaultAzureCredential()』 類別,可讓您在不同的環境中使用相同的程式代碼,並提供身分識別。
credential = DefaultAzureCredential()
寫入機密總帳
您現在可以使用 PostLedgerEntry 方法寫入機密總帳。
Operation postOperation = ledgerClient.PostLedgerEntry(
waitUntil: WaitUntil.Completed,
RequestContent.Create(
new { contents = "Hello world!" }));
取得交易標識碼
PostLedgerEntry 方法會傳回物件,其中包含您剛寫入機密總帳之專案的交易。 若要取得交易標識碼,請存取 「Id」 值:
string transactionId = postOperation.Id;
Console.WriteLine($"Appended transaction with Id: {transactionId}");
從機密總帳讀取
使用交易標識碼,您也可以使用 GetLedgerEntry 方法從機密總帳讀取:
Response ledgerResponse = ledgerClient.GetLedgerEntry(transactionId, collectionId);
string entryContents = JsonDocument.Parse(ledgerResponse.Content)
.RootElement
.GetProperty("entry")
.GetProperty("contents")
.GetString();
Console.WriteLine(entryContents);
測試並驗證
直接在控制台中,執行下列命令以執行應用程式。
dotnet run
範例指令碼
using System;
using Azure.Core;
using Azure.Identity;
using Azure.Security.ConfidentialLedger;
using Azure.Security.ConfidentialLedger.Certificate;
namespace acl_app
{
class Program
{
static Task Main(string[] args)
{
// Replace with the name of your confidential ledger
const string ledgerName = "myLedger";
var ledgerUri = $"https://{ledgerName}.confidential-ledger.azure.com";
// Create a confidential ledger client using the ledger URI and DefaultAzureCredential
var ledgerClient = new ConfidentialLedgerClient(new Uri(ledgerUri), new DefaultAzureCredential());
// Write to the ledger
Operation postOperation = ledgerClient.PostLedgerEntry(
waitUntil: WaitUntil.Completed,
RequestContent.Create(
new { contents = "Hello world!" }));
// Access the transaction ID of the ledger write
string transactionId = postOperation.Id;
Console.WriteLine($"Appended transaction with Id: {transactionId}");
// Use the transaction ID to read from the ledger
Response ledgerResponse = ledgerClient.GetLedgerEntry(transactionId, collectionId);
string entryContents = JsonDocument.Parse(ledgerResponse.Content)
.RootElement
.GetProperty("entry")
.GetProperty("contents")
.GetString();
Console.WriteLine(entryContents);
}
}
}
下一步
若要深入瞭解 Azure 機密總帳,以及如何將其與您的應用程式整合,請參閱下列文章: