Neo
Neo

Reputation: 16219

how to generate token from azure AD app client id?

I have one application which is register into azure AD. I have client id with me and secret key is inside the key vault.

How to access that secure Azure AD register api using console app ?

I guess i need a bearer token for it how to generate it?

I search on and I got something like below code -

var authority = "https://login.microsoftonline.com/your-aad-tenant-id/oauth2/token";
var context = new AuthenticationContext(authority);
var resource = "https://some-resource-you-want-access-to";

var clientCredentials = new ClientCredential(clientId, clientSecret);

var result = await context.AcquireTokenAsync(resource, clientCredentials);  

Upvotes: 1

Views: 4482

Answers (1)

Joy Wang
Joy Wang

Reputation: 42043

You could try the code below to generate the token, in my sample, I generate the token for https://graph.microsoft.com.

string graphResourceId = "https://graph.microsoft.com/";
string authority = "https://login.microsoftonline.com/your-aad-tenant-id/oauth2/token";
string tenantId = "your-aad-tenant-id";
string clientId = "your-clientid";
string secret = "your-secret";
authority = String.Format(authority, tenantId);
AuthenticationContext authContext = new AuthenticationContext(authority);
var accessToken = authContext.AcquireTokenAsync(graphResourceId, new ClientCredential(clientId, secret)).Result.AccessToken;

enter image description here

Upvotes: 1

Related Questions