|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Microsoft.Identity.Client; |
| 5 | +using Microsoft.Identity.Client.AppConfig; |
| 6 | +using Microsoft.IdentityModel.Abstractions; |
| 7 | +using System; |
| 8 | +using System.Security.Cryptography.X509Certificates; |
| 9 | +using System.Runtime.InteropServices; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +internal class Program |
| 13 | +{ |
| 14 | + private static async Task Main() |
| 15 | + { |
| 16 | + // ─── 0. Environment banner ──────────────────────────────────────────────── |
| 17 | + Console.WriteLine(new string('═', 70)); |
| 18 | + |
| 19 | + // ─── 1. Certificate events ──────────────────────────────────────────────── |
| 20 | + ManagedIdentityApplication.BindingCertificateUpdated += c => |
| 21 | + { |
| 22 | + Console.WriteLine($"\n[{Now()}] Event Fired - BindingCertificateUpdated:"); |
| 23 | + PrintCertificate(c); |
| 24 | + }; |
| 25 | + |
| 26 | + // ─── 2. Managed-identity source & certs ─────────────────────────────────── |
| 27 | + var source = await ManagedIdentityApplication.GetManagedIdentitySourceAsync() |
| 28 | + .ConfigureAwait(false); |
| 29 | + Console.WriteLine($"\n[{Now()}] Managed Identity Source: {source}"); |
| 30 | + |
| 31 | + var cert = ManagedIdentityApplication.GetManagedIdentityBindingCertificate(); |
| 32 | + PrintCertificate(cert, "Initial Managed-Identity Binding Certificate"); |
| 33 | + |
| 34 | + //sleep for 5 second so we can see different timestamps |
| 35 | + Thread.Sleep(5000); |
| 36 | + |
| 37 | + cert = ManagedIdentityApplication.ForceUpdateInMemoryCertificate(); |
| 38 | + PrintCertificate(cert, "Forced NEW Managed-Identity Binding Certificate"); |
| 39 | + |
| 40 | + // ─── 3. Build MI app ────────────────────────────────────────────────────── |
| 41 | + IIdentityLogger logger = new IdentityLogger(); |
| 42 | + IManagedIdentityApplication mi = ManagedIdentityApplicationBuilder |
| 43 | + .Create(ManagedIdentityId.SystemAssigned) |
| 44 | + .WithExperimentalFeatures() |
| 45 | + .WithLogging(logger, true) |
| 46 | + .Build(); |
| 47 | + |
| 48 | + // ─── 4. Token-acquisition loop ─────────────────────────────────────────── |
| 49 | + string scope = "https://vault.azure.net/"; |
| 50 | + while (true) |
| 51 | + { |
| 52 | + Console.WriteLine($"\n[{Now()}] Acquiring token for scope → {scope}"); |
| 53 | + try |
| 54 | + { |
| 55 | + var result = await mi.AcquireTokenForManagedIdentity(scope) |
| 56 | + //.WithProofOfPossession() |
| 57 | + .ExecuteAsync() |
| 58 | + .ConfigureAwait(false); |
| 59 | + |
| 60 | + Console.WriteLine($"[{Now()}] ✅ Success (expires {result.ExpiresOn:HH:mm:ss})"); |
| 61 | + Console.WriteLine($"Access-Token (first 100 chars): {result.AccessToken[..100]}…"); |
| 62 | + } |
| 63 | + catch (MsalServiceException ex) |
| 64 | + { |
| 65 | + Console.ForegroundColor = ConsoleColor.Red; |
| 66 | + Console.WriteLine($"[{Now()}] ❌ {ex.ErrorCode}: {ex.Message}"); |
| 67 | + Console.ResetColor(); |
| 68 | + } |
| 69 | + |
| 70 | + Console.Write("\nEnter new scope or 'q' to quit: "); |
| 71 | + var input = Console.ReadLine(); |
| 72 | + if (string.Equals(input, "q", StringComparison.OrdinalIgnoreCase)) |
| 73 | + break; |
| 74 | + if (!string.IsNullOrWhiteSpace(input)) |
| 75 | + scope = input.Trim(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // ─── Helpers ──────────────────────────────────────────────────────────────── |
| 80 | + |
| 81 | + private static string Now() => DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss"); |
| 82 | + |
| 83 | + private static void PrintCertificate(X509Certificate2 cert, string? title = null) |
| 84 | + { |
| 85 | + Console.WriteLine("\n" + (title ?? "Certificate details") + "\n" + new string('-', 60)); |
| 86 | + Console.WriteLine($"Subject : {cert.Subject}"); |
| 87 | + Console.WriteLine($"Issuer : {cert.Issuer}"); |
| 88 | + Console.WriteLine($"Serial # : {cert.SerialNumber}"); |
| 89 | + Console.WriteLine($"Not Before : {cert.NotBefore:yyyy-MM-dd HH:mm:ss}"); |
| 90 | + Console.WriteLine($"Not After : {cert.NotAfter:yyyy-MM-dd HH:mm:ss}"); |
| 91 | + Console.WriteLine($"Thumbprint : {cert.Thumbprint}"); |
| 92 | + Console.WriteLine(new string('-', 60)); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/// <summary>Minimal ILogger that pipes everything to Console.</summary> |
| 97 | +class IdentityLogger : IIdentityLogger |
| 98 | +{ |
| 99 | + public EventLogLevel MinLogLevel => EventLogLevel.Verbose; |
| 100 | + public bool IsEnabled(EventLogLevel level) => level <= MinLogLevel; |
| 101 | + |
| 102 | + public void Log(LogEntry entry) => Console.WriteLine($"[{DateTimeOffset.Now:HH:mm:ss}] {entry.Message}"); |
| 103 | +} |
0 commit comments