共用方式為


快速入門:適用於 .NET 的 Azure 機密總帳用戶端程式庫

開始使用適用於 .NET 的 Azure 機密總帳用戶端程式庫。 Azure 機密分類帳 是一項新的高度安全服務,用於管理敏感性資料記錄。 根據許可的區塊鏈模型,Azure 機密總帳提供獨特的資料完整性優勢。 其中包括不變性、讓總帳僅限附加及防篡改,以確保所有記錄都保持不變。

在本快速入門中,您將瞭解如何使用 .NET 用戶端程式庫在 Azure 機密總帳中建立專案

Azure 機密總帳用戶端程式庫資源:

API 參考文件 | 程式庫原始程式碼 | 套件 (NuGet)

先決條件

您也需要執行中的機密總帳,以及具有 Administrator 權限的已註冊使用者。 您可以使用 Azure 入口網站Azure CLIAzure PowerShell 來建立機密分類帳 (和系統管理員)。

設定

建立新的 .NET 主控台應用程式

  1. 在命令殼層中,執行下列命令以建立名為 acl-app 的專案:

    dotnet new console --name acl-app
    
  2. 變更為新建立的 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://<your-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!" }));

取得交易 ID

PostLedgerEntry 方法會回傳一個物件,其中包含您剛才寫入至機密分類帳的交易紀錄。 若要取得交易 ID,請存取「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 機密總帳,以及如何將它與您的應用程式整合,請參閱下列文章: