Hi. I can't found an working example in .net core, to obtain the bearer token.
I have done all the steps described in https://developer.ci.ai.dynamics.com/
I have this code
public static async Task<AuthenticationResult> GetTokenAsync(ILogger log, bool isRenew = false)
{
String clientId = Environment.GetEnvironmentVariable("CI-ClientId");
String clientSecret = Environment.GetEnvironmentVariable("CI-ClientSecret");
String tenant = Environment.GetEnvironmentVariable("AzureTenantId");
log.LogInformation("Obteniendo token");
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(new Uri($"">login.microsoftonline.com/.../token"))
.Build();
string[] scopes = new string[] { "">graph.microsoft.com/.default" };
AuthenticationResult result = null;
result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
if (DateTime.Now > result.ExpiresOn.AddMinutes(-15) && isRenew == false)
{
log.LogInformation("Token pronto a expirar, se renueva");
result = await GetTokenAsync(log, true);
}
return result;
}:
The Token is returned OK, but, if I try to use it, for example in postam, I have an error:
Bearer error="invalid_token", error_description="The signature is invalid"
If I use the token generated from the swagger, it works fine
I have notticed, that the token generated by code, is shorten tan the swagger.
Hi. I found the problem in the WithAuthority url, and the scope.
This is the final code to get a working token
String clientId = Environment.GetEnvironmentVariable("CI-ClientId");
String clientSecret = Environment.GetEnvironmentVariable("CI-ClientSecret");
String tenant = Environment.GetEnvironmentVariable("AzureTenantId");
log.LogInformation("Obteniendo token");
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(new Uri($"login.microsoftonline.com/{tenant}"))
.Build();
string[] scopes = new string[] { "api.ci.ai.microsoft.com/.default" };
AuthenticationResult result = null;
result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
André Arnaud de Cal...
292,111
Super User 2025 Season 1
Martin Dráb
230,934
Most Valuable Professional
nmaenpaa
101,156