다음을 통해 공유


빠른 시작: .NET용 Azure 기밀 원장 클라이언트 라이브러리

.NET용 Azure 기밀 원장 클라이언트 라이브러리를 시작합니다. Azure 기밀 원장은 중요한 데이터 레코드를 관리하기 위한 새롭고 매우 안전한 서비스입니다. 권한 있는 블록체인 모델을 기반으로 Azure 기밀 원장은 고유한 데이터 무결성 이점을 제공합니다. 여기에는 원장을 추가 전용으로 만드는 불변성, 모든 레코드가 그대로 유지되도록 하는 변조 방지가 포함됩니다.

이 빠른 시작에서는 .NET 클라이언트 라이브러리를 사용하여 Azure 기밀 원장에 항목을 만드는 방법을 알아봅니다.

Azure Confidential Ledger 클라이언트 라이브러리 리소스:

API 참조 설명서 | 라이브러리 소스 코드 | 패키지(NuGet)

필수 조건

또한 실행 중인 기밀 장부와 Administrator 권한이 있는 등록된 사용자가 필요합니다. Azure Portal, Azure CLI 또는 AzurePowerShell을 사용하여 기밀 원장(및 관리자)을 만들 수 있습니다.

설치 프로그램

새 .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 기밀 원장 클라이언트 라이브러리를 사용하면 서비스에서 변경할 수 없는 원장 항목을 만들 수 있습니다. 코드 예제 섹션에서는 원장에 대한 쓰기를 만들고 트랜잭션 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" 형식의 Key Vault URI로 확장됩니다. 이 예제에서는 Azure ID 라이브러리'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}");

Confidential Ledger에서 읽기

트랜잭션 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 기밀 원장 및 앱과 통합하는 방법에 대한 자세한 내용은 다음 문서를 참조하세요.