Skrymsli
Skrymsli

Reputation: 5313

How do I get the ObjectID associated with the authenticated Azure service principal?

I created an Enterprise Application in Azure AD. I'm using that to authenticate and create resources:

AzureCredentials creds = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
    ClientId, ClientSecret, TenantId, AzureEnvironment.AzureGlobalCloud);

IAzure az = Azure.Authenticate(creds);

I don't know how to get the ObjectId associated with the Enterprise Application that I used to authenticate. Is there a way?

Upvotes: 1

Views: 1308

Answers (1)

Joy Wang
Joy Wang

Reputation: 42043

You could use Microsoft Graph SDK to do that, refer to the sample below, it calls the Microsoft Graph API - List servicePrincipals, filter with appId i.e. ClientId.

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var servicePrincipals = await graphClient.ServicePrincipals
    .Request().Filter("appId eq '<ClientId here>'")
    .GetAsync();

When using the code, choose the authProvider you want, and make sure you have enough permission to call the API, it works fine on my side, the Id is the ObjectId of the service principal i.e. enterprise application.

enter image description here

Upvotes: 2

Related Questions