次の方法で共有


クイック スタート: .NET 用 Azure Confidential Ledger クライアント ライブラリ

.NET 用 Azure Confidential Ledger クライアント ライブラリの使用を開始します。 Azure Confidential Ledger は、機密データ レコードを管理するための新しいセキュリティで保護されたサービスです。 許可されたブロックチェーン モデルに基づいて、Azure 機密台帳には固有のデータ整合性の利点があります。 これには、不変性、台帳の追加のみの作成、および改ざんの証明が含まれます。これにより、すべてのレコードが損なわれないようにすることができます。

このクイック スタートでは、.NET クライアント ライブラリを使用して Azure 機密台帳にエントリを作成する方法について説明します

Azure Confidential Ledger クライアント ライブラリ リソース:

API リファレンス ドキュメント | ライブラリのソース コード | パッケージ (NuGet)

[前提条件]

また、実行中の機密台帳と、 Administrator 特権を持つ登録済みユーザーも必要です。 Azure portalAzure CLI、または Azure 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 用 Confidential Ledger クライアント ライブラリをインストールします。

dotnet add package Azure.Security.ConfidentialLedger --version 1.0.0

このクイック スタートでは、Azure ID 用の Azure SDK クライアント ライブラリもインストールする必要があります。

dotnet add package Azure.Identity

オブジェクト モデル

.NET 用 Azure Confidential Ledger クライアント ライブラリを使用すると、サービスに不変の台帳エントリを作成できます。 コード セクションでは、台帳への書き込みを作成し、トランザクション ID を取得する方法を示します。

コード例

ディレクティブを追加する

Program.csの先頭に 次のディレクティブを追加します。

using System;
using Azure.Core;
using Azure.Identity;
using Azure.Security.ConfidentialLedger;
using Azure.Security.ConfidentialLedger.Certificate;

クライアントの認証と作成

このクイック スタートでは、ログイン ユーザーを使用して Azure Confidential Ledger に対する認証を行います。これは、ローカル開発に推奨される方法です。 機密台帳の名前は、"https://<your-confidential-ledger-name>.confidential-ledger.azure.com" の形式でキー ボルト URI に拡張されます。 この例では、Azure Identity Library'DefaultAzureCredential()' クラスを使用しています。これにより、異なるオプションを持つさまざまな環境で同じコードを使用して ID を提供できます。

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}");

機密台帳から読み取る

トランザクション ID を使用すると、 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 Confidential Ledger の詳細とアプリと統合する方法については、次の記事を参照してください。