ASP.NET Framework MSGraph empty GetAccountsAsync()

Preishuber 1 Reputation point
2021-12-31T10:18:40.917+00:00

woking on prototype with VB (and c#) webforms (or MVC doenst matter)

stepping in startup -seems to work

   app.UseOpenIdConnectAuthentication(New OpenIdConnectAuthenticationOptions() With {
            .ClientId = clientId,
            .Authority = authority,
            .PostLogoutRedirectUri = postLogoutRedirectUri,
            .Notifications = New OpenIdConnectAuthenticationNotifications() With {
              .AuthorizationCodeReceived = Function(context)

                                               Dim code = context.Code   '.code OpenIdConnect 4.2 AuthorizationCodeReceived
                                               Dim credential As New ClientCredential(clientId, ClientSecret)
                                               Dim signedInUserID As String = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value

                                               Dim authContext As New AuthenticationContext(authority, New ADALTokenCache(signedInUserID))
                                               Dim result As AuthenticationResult = authContext.AcquireTokenByAuthorizationCodeAsync(code, New Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId).Result

                                               Return Task.FromResult(0)
                                           End Function
              }
        })

Database is generated- entry in table

generating a aspx page

  Dim publicClient = ConfidentialClientApplicationBuilder.Create(Startup.clientId) _
            .WithClientSecret(ConfigurationManager.AppSettings("ida:ClientSecret")) _
               .WithAuthority(New Uri(authorityUri)) _
        .WithRedirectUri(redirectUri).Build()
         Dim accounts = Await publicClient.GetAccountsAsync() 'Empty
        Try
            Dim res1 = publicClient.AcquireTokenSilent(scopes, accounts.FirstOrDefault) _

so acquire token cache throws an exception-
thanks for help

Microsoft Security Microsoft Entra Microsoft Entra ID
Developer technologies ASP.NET Other
{count} votes

1 answer

Sort by: Most helpful
  1. JamesTran-MSFT 36,906 Reputation points Microsoft Employee Moderator
    2022-01-05T19:09:11.367+00:00

    Thank you for following up on and I apologize for the misunderstanding!

    Based off our Get a token from the token cache using MSAL.NET documentation "when you acquire an access token using (MSAL.NET), the token is cached. When the application needs a token, it should first call the AcquireTokenSilent method to verify if an acceptable token is in the cache. The recommended pattern is to call the AcquireTokenSilent method first. If AcquireTokenSilent fails, then acquire a token using other methods". For more info.

    Can you try using AcquireTokenInteractive, similar to our doc, to acquire a token? I also found some documentation on regarding AcquireTokenForClient, which says don't call AcquireTokenSilent before you call AcquireTokenForClient, because AcquireTokenSilent uses the user token cache. For more info.
    162647-image.png

    I hope this helps!
    Thank you again for your time and patience throughout this issue.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.