We are trying to access the calendar events of the current user from a asp.net core 2.2 web api project. We are using Azure AD authentication and that JWT token is passed by the Angular application. I'm using the following code to get the Graph token.
var token = Request.Headers["Authorization"].ToString();
var app = ConfidentialClientApplicationBuilder.Create(config.ClientId) // Has GUID of registered app
.WithAuthority(config.Authority) // URL + Tenant ID
.Build();
// THIS LINE THROWS THE EXCEPTION
var graphToken = await app.AcquireTokenOnBehalfOf(new List<string> { "Calendars.ReadWrite" },
new Microsoft.Identity.Client.UserAssertion(token)).ExecuteAsync();
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", graphToken.AccessToken);
var response = await client.GetAsync("https://graph.microsoft.com/v1.0/me");
var content = await response.Content.ReadAsStringAsync();
return Ok(content);
Not really sure why I'm getting this error. token has the JWT generated by Azure AD. I have tried with and without the Bearer prefix of the token with the same result.
Thank you,