ไทย/Eng
โพสต์นี้ จะพูดเกี่ยวกับ การดึงข้อมูลของ “Key”, “Secret”, “Certificate” จาก Azure KeyVault โดยใช้ C#
สิ่งที่ต้องเตรียม
- Azure Portal Subscription Account – ถ้ายังไม่มี. ลองที่นี่
- Azure KeyVault with generated certificate – ถ้ายังไม่ได้ทำ
- Visual Studio – ในโพสต์นี้ใช้ VS2017 Preview 2 with .NET Version 4.6.1
มาเริ่มกันเลย
มี 2 สิ่งที่ต้องทำ คือ:
- เตรียมความพร้อม – ตั้งค่า Azure KeyVault and Azure ActiveDirectory.
- พัฒนาแอปสำหรับดึงข้อมูล – ดึง Certificate จาก Azure KeyVault.
โค้ดฉบับเต็มสำหรับโพสต์นี้ อัพโหลดไว้ที่ GitHub
-
- เตรียมความพร้อม
อย่างแรก, คุณต้องทำการเปิดสิทธิ์ในการเข้าถึง KeyVault ใน Resource Group ของคุณก่อน.
-
-
- ไปที่ Azure Portal, จากนั้น ไปที่ “Azure Active Directory” ในส่วนของ “App registrations” คลิกที่ “New application registration”
-
-
-
- กำหนด “Name” และ “Sign-on URL”(ไม่จำเป็นต้องใช้ได้จริง แต่ต้องมี). สำหรับ “Application Type” ต้องเป็น “Web app/ API” ไม่เช่นนั้น จะสร้าง Key สำหรับการยืนยันตัวตนไม่ได้
-
-
-
- หลังจากเสร็จเรียบร้อยแล้ว คุณจะเห็น “Application ID” ซึ่งนี่จะเป็น ClientId สำหรับ โปรแกมที่จะพัฒนาต่อไป
-
-
-
- ต่อไป คลิกปุ่ม “Settings” ตามรูปข้างล่าง จากนั้นไปที่ “Keys” กำหนด “description” และเลือก “expires” เมื่อเสร็จแล้ว คลิกปุ่ม “Save”
-
-
-
- จากนั้น ข้อความ secret จะปรากฏขึ้นมา ทันทีที่บันทึกเสร็จเรียบร้อย ซึ่งนี่จะเป็น ClientSecret สำหรับโปรแกรมที่จะพัฒนาต่อไป
-
คุณต้องก็อปปี้เก็บไว้ทันที เพราะว่าข้อความนี้จะปรากฏแค่ครั้งเดียวเท่านั้น ไม่สามารถกลับมาดูย้อนหลังได้อีก นอกเสียจากจะลบ แล้วสร้างใหม่
สำหรับการสร้างตัวตนใน Active Directory สำหรับการเข้าถึงแอปต่างๆ ใน Resource Group เสร็จเรียบร้อยแล้ว แต่ ณ ตอนนี้ ยังไม่สามารถเข้าถึง KeyVault ได้ ยังเหลืออีกสิ่งหนึ่งที่ต้องทำ คือการเพิ่มตัวตนนี้ ให้มีสิทธิ์เข้าถึง KeyVaut ของเราต่อไป
-
-
- ไปที่ Azure KeyVault. หากยังไม่ได้ทำ คลิกที่นี่ จากนั้นไปที่ “Access Policies” คลิกปุ่ม “Add New”.
-
-
-
- ในส่วนของตัวเลือก “Configure from template” ให้เลือก “Key, Secret, & Certificate Management” จากนั้น “Select Principal” เลือกตัวตนที่สร้างไว้ใน Active Directory จากขั้นตอนก่อนหน้านี้
-
-
-
- ถึงตรงนี้ เราพร้อมที่จะพัฒนาโปรแกรมที่จะมาเรียกข้อมูลแล้ว
- พัฒนาแอปสำหรับดึงข้อมูล
- เปิด Visual Studio และสร้างโปรเจ็คใหม่
-
-
-
- ก่อนอื่น คุณต้องติดตั้ง NyGet 2 แพกเกจ ได้แก่ “Microsoft.Azure.KeyVault”, “Microsoft.IdentityModel.Clients.ActiveDirectory”
-
-
-
- ประกาศตัวแปรคงที่ CLIENT_ID and CLIENT SECRET
-
const string CLIENT_ID = "Key from step 1.3"; const string CLIENT_SECRET = "Key from step 1.5";
-
-
- เขียนเมธอดสำหรับการยืนยันตัวตนด้วย ActiveDirectory
-
static KeyVaultClient GetClient() => new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(async (string authority, string resource, string scope) => { var context = new AuthenticationContext(authority, TokenCache.DefaultShared); ClientCredential clientCred = new ClientCredential(CLIENT_ID, CLIENT_SECRET); var authResult = await context.AcquireTokenAsync(resource, clientCred); return authResult.AccessToken; }));
-
-
- เราสามารถอ้างถึง certificates ใน KeyVault ได้ 2 แบบ คือ
- ใช้ Certificate Identifier. โดย Certificate Identifier สามารถหาได้จากรูปด้านล่าง
- เราสามารถอ้างถึง certificates ใน KeyVault ได้ 2 แบบ คือ
-
const string CERTIFICATE_IDENTIFIER = "https://key-vault-just-for-test.vault.azure.net/certificates/TestCertificate/ee3c947cdb5241c2a7f711f9770b669e";
-
-
-
- ใช้ KeyVaule Identifier + Certificate Name
-
-
const string KEY_VAULT_IDENTIFIER = "https://key-vault-just-for-test.vault.azure.net/"; const string CERTIFICATE_NAME = "TestCertificate";
-
-
- เขียนเมธอด Main ดังต่อไปนี้
-
static void Main(string[] args) { var Client = GetClient(); var Certificate = Client.GetCertificateAsync(CERTIFICATE_IDENTIFIER).GetAwaiter().GetResult(); Console.WriteLine(Certificate.X509Thumbprint.ToHexString()); var Certificate2 = Client.GetCertificateAsync(KEY_VAULT_IDENTIFIER, CERTIFICATE_NAME).GetAwaiter().GetResult(); Console.WriteLine(Certificate2.X509Thumbprint.ToHexString()); }
-
-
- ในโปรเจ็คนี้ ผมได้เขียน ส่วนขยายคลาส (Extension) “string” เพื่อแปลงค่าจาก byte array เป็น hex string โดยคลาสส่วนขยาย เขียนได้ดังนี้
-
public static class StringExtend { public static string ToHexString(this byte[] hex) { if (hex == null) return null; if (hex.Length == 0) return string.Empty; var s = new StringBuilder(); foreach (byte b in hex) { s.Append(b.ToString("x2")); } return s.ToString(); } }
-
-
- ถึงตรงนี้ โปรเจ็คเราพร้อมที่จะรันแล้ว โดยผลลัพธ์ สามารถแสดงได้ดังต่อไปนี้
-
นอกจากจะดึงข้อมูลมาแสดงแล้ว เรายังสามารถใช้ ไลบรารี เหล่านี้ในการ สร้าง แก้ไข หรือ ลบ ข้อมูลใน KeyVault ได้อีกด้วย
ขอบคุณที่เข้ามาอ่าน แล้วเจอกันใหม่ โพสต์หน้า
Happy Coding.