快速入門:適用于 .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)
安裝套件
使用 [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 機密總帳,以及如何將其與您的應用程式整合,請參閱下列文章: