question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot acquire token in web app context.

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jmprieurcommented, May 9, 2018

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.

2reactions
jmprieurcommented, May 9, 2018

@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 return Task<HttpResponseMessage>, and then change the following line to:

var user = await graphServiceClient.Me.Request().GetAsync();

GetAuthenticatedClient() does not seem async by itself so I think it’s ok. HTH

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found