using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
public enum ManagedIdentityType
public class ManagedIdentityInfo
public ManagedIdentityType Type { get; }
public string TenantId { get; }
public string ClientId { get; }
public string PrincipalId { get; }
public string IdentityUrl { get; }
public string AuthenticationEndpoint { get; }
public byte[] CertBytes { get; }
public string? ResourceId { get; }
public ManagedIdentityInfo(ManagedIdentityType type,
string authenticationEndpoint,
string? resourceId = null)
this.TenantId = tenantId;
this.ClientId = clientId;
this.PrincipalId = principalId;
this.IdentityUrl = identityUrl;
this.AuthenticationEndpoint = authenticationEndpoint;
this.CertBytes = certBytes;
this.ResourceId = resourceId;
public static void Main()
byte[] byteArr1 = Convert.FromBase64String(
"MIIB4TCCAYugAwIBAgIUfGEXH+S4DkLlKaHGspISGaqFW4MwDQYJKoZIhvcNAQEL" +
"BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM" +
"GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMjEwMTkyMjExNDFaFw0yMzEw" +
"MTkyMjExNDFaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw" +
"HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwXDANBgkqhkiG9w0BAQEF" +
"AANLADBIAkEAnsfAhlpNeE+Da2jdaahSJw30ch+JDgVwKsy3kSI1OKq+Ix0GpQ+A" +
"9m7FgUsAq48me9TmbKPYOFFq3p+8/czGGQIDAQABo1MwUTAdBgNVHQ4EFgQURPv+" +
"G/6/643OMx8g9KXjXnyhHvQwHwYDVR0jBBgwFoAURPv+G/6/643OMx8g9KXjXnyh" +
"HvQwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAANBAFpl5G3AxFKX8B88" +
"bulxPq6dWPVIsJISuL9RklyXfpiRngWWt8c2HWWm/kWKCYWPaYwNGd9GVkqqgY2d" +
List<ManagedIdentityInfo> siteIdentities = new List<ManagedIdentityInfo>();
siteIdentities.Add(new ManagedIdentityInfo(type: ManagedIdentityType.SystemAssigned,
principalId: "X1principalId",
identityUrl: "X1identityUrl",
authenticationEndpoint: "X1authenticationEndpoint",
siteIdentities.Add(new ManagedIdentityInfo(type: ManagedIdentityType.UserAssigned,
principalId: "X2principalId",
identityUrl: "X2identityUrl",
authenticationEndpoint: "X2authenticationEndpoint",
resourceId: "X2resourceId"));
var siteIdentityMap = new Dictionary<string, IList<ManagedIdentityInfo>>
{ "siteIdentities", siteIdentities }
var serializedSiteIdentityMap = System.Text.Json.JsonSerializer.Serialize(siteIdentityMap, new JsonSerializerOptions
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
Console.WriteLine($"Serialized content using System.Text.Json.JsonSerializer: {serializedSiteIdentityMap}");
var serializedSiteIdentitiesStr = JObject.Parse(serializedSiteIdentityMap).SelectToken("siteIdentities")?.ToString();
foreach (var jtoken in JArray.Parse(serializedSiteIdentitiesStr))
JObject jObject = jtoken as JObject;
var jtok = jObject.GetValue("certBytes", StringComparison.OrdinalIgnoreCase);
Console.WriteLine(jtok.Type);
int type = jObject.GetValue("type", StringComparison.OrdinalIgnoreCase).Value<int>();
string tenantId = jObject.GetValue("tenantId", StringComparison.OrdinalIgnoreCase)?.Value<string>();
string clientId = jObject.GetValue("clientId", StringComparison.OrdinalIgnoreCase)?.Value<string>();
string principalId = jObject.GetValue("principalId", StringComparison.OrdinalIgnoreCase)?.Value<string>();
string identityUrl = jObject.GetValue("identityUrl", StringComparison.OrdinalIgnoreCase)?.Value<string>();
string authenticationEndpoint = jObject.GetValue("authenticationEndpoint", StringComparison.OrdinalIgnoreCase)?.Value<string>();
byte[] certBytes = jObject.GetValue("certBytes", StringComparison.OrdinalIgnoreCase)?.ToObject<byte []>();
string resourceId = jObject.GetValue("resourceId", StringComparison.OrdinalIgnoreCase)?.Value<string>();
var sb = new StringBuilder(certBytes.Length);
foreach(var b in certBytes)
sb.Append(b).Append(",");
var certBytesStr = sb.ToString();
Console.WriteLine($"Deserialized and extracted content: {type}, {tenantId}, {clientId}, {principalId}, {identityUrl}, {authenticationEndpoint}, {certBytesStr}, {resourceId}");