1

currently, we are developing an Azure app service application, which has a system managed identity assigned during setup of the app service. We use the managed identitiy with RBAC to access other Azure resources and that is working fine.

Now I want to get some information from the underlying managed identity to perform some checks. Especially I want to read the application id, which is assigned to this managed identity. I want to do that in C#. How can I access this information?

Any help appreciated. Thanks in advance.

Regards, Stati

Stati
  • 45
  • 1
  • 4
  • Are you using ARM for deployment? – Ankush Jain Mar 20 '21 at 09:42
  • do you want to read it from within the app itself? – silent Mar 20 '21 at 09:47
  • Yes, I want to read it inside the app. The main reason is that I want to read the application id from within the running app server, encrypt it via public key and send it to a global validation service, which does not run inside the same AD. – Stati Mar 21 '21 at 08:15

1 Answers1

0
    var credential = new DefaultAzureCredential();
    string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
    var token = await credential.GetTokenAsync(new Azure.Core.TokenRequestContext(scopes));

    var handler = new JwtSecurityTokenHandler();
    var jsonToken = handler.ReadToken(token.Token) as JwtSecurityToken;
    var appid = jsonToken.Claims.First(c => c.Type == "appid").Value;
    Console.WriteLine(appid);
Cindy Pau
  • 13,085
  • 1
  • 15
  • 27