1

I tried to find an Azure authentication mechanism that uses an access token as a parameter. but no luck. I just found ClientSecretCredential class that use tenant id, client id, and client secret as a parameter like below :

var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);

the reason I need the that is the access token will be generated by another service and my service will only accept access token to be used to authenticate Azure AD.

Actually, I can utilize Azure Management RestAPI to do that. However to improve developer experience I'd like to utilize .NET client library if possible.

I have tried to find documentation in Azure Identity client library in https://learn.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme?view=azure-dotnet, but I couldn't find any class or method that I need.

Dika Arta Karunia
  • 476
  • 1
  • 4
  • 17

1 Answers1

1
  • If you want tokens from azure ad you can use using Microsoft.IdentityModel.Clients.ActiveDirectory; library to get the tokens.

  • I am assuming that you have already created an azure ad app registration and already possess the client_id , client_secret and tenant_id. Just save this as strings in code.

  • Now we can use the clientcredential along with authenticationContext we can acquire tokens.

Complete program :

string client_id = "";
string client_secret = "";
string tenant_id = "";
string endpoint = "https://login.microsoftonline.com/"+tenant_id;



ClientCredential credent = new ClientCredential(client_id , client_secret);

var context = new AuthenticationContext(endpoint);
            var result = context.AcquireTokenAsync("https://management.azure.com/",credent);

var result = context.AcquireTokenAsync("https://management.azure.com/",credent);

Console.WriteLine(result.Result.AccessToken);

Here result.Result.AccessToken will give you a access token in form of a token.

enter image description here

Mohit Ganorkar
  • 1,917
  • 2
  • 6
  • 11
  • Hi @MohitGanorkar, thanks for your answer, sorry if my question not clear enough, actually your code above is my another service that produce the access token, my question is , is there any .net client library mechanism to authenticate by using the provided token above? thanks. – Dika Arta Karunia Nov 14 '22 at 14:27