Cannot acquire token in web app context.
See original GitHub issueI am running a web app hosted in iis express at http://localhost:52419/ and the calls get stuck in token generation forever. But when I test the method from console, it all works fine : there is no problem with token generation (await daemonClient.AcquireTokenForClientAsync(new[] {msGraphScope});.
<add key="AuthorityFormat" value="https://login.microsoftonline.com/{0}/v2.0" /> <add key="RedirectUri" value="https://localhost:44316/" /> <add key="MsGraphScope" value="https://graph.microsoft.com/.default" />
public static GraphServiceClient GetAuthenticatedClient(ApplicationIdentity applicationIdentity,
string tenantId)
{
var authorityFormat = ConfigurationManager.AppSettings["AuthorityFormat"];
var redirectUri = ConfigurationManager.AppSettings["RedirectUri"];
var msGraphScope = ConfigurationManager.AppSettings["MsGraphScope"];
var graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
async requestMessage =>
{
var daemonClient =
new ConfidentialClientApplication(applicationIdentity.Id,
string.Format(authorityFormat, tenantId), redirectUri,
new ClientCredential(applicationIdentity.Secret),
null, new TokenCache());
var authResult =
await daemonClient.AcquireTokenForClientAsync(new[] {msGraphScope}); // When run application as web then the call gets stuck here forever.
// Append the access token to the request.
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("bearer", authResult.AccessToken);
}));
return graphClient;
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (4 by maintainers)
Top Results From Across the Web
Unable to get the authentication token after redirecting ...
1 Answer. The first parameter of acquireToken method is the value of a resource which you want to access.It is the App ID...
Read more >WARNING: Unable to acquire token for tenant - ...
When i am trying to connect to my azure portal through powershell iam getting the error: WARNING: Unable to acquire token for tenant....
Read more >A web app that calls web APIs: Acquire a token for the app
To get this token, you call the Microsoft Authentication Library (MSAL) AcquireTokenSilent method (or the equivalent in Microsoft.Identity.Web).
Read more >How to get an access token with Authorization Code Grant
The acquired access token in the request's Authorization header. The base URI for the request, built from the account_id and the base_uri of...
Read more >ID Token and Access Token: What's the Difference?
An ID token is encoded as a JSON Web Token (JWT), a standard format that allows your application to easily inspect its content,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
glad to read it did, @sameerkattel, and thanks for the update! @jennyf19 : we need to be explicit about this in the conceptual documentation (in the Client Creds section, add a paragraph for ASP.NET controllers that need to be async, and a troubleshooting FAQ for MT.
@sameerkattel. I suspect you are experiencing a MT issue. I don’t think that you should use .Result(). See Reminder: Do not call ADAL or MSAL async methods synchronously from the UI thread
I’d advise you change your controller action to be
async
and returnTask<HttpResponseMessage>
, and then change the following line to:GetAuthenticatedClient() does not seem async by itself so I think it’s ok. HTH