question

Preishuber-1903 avatar image
0 Votes"
Preishuber-1903 asked JamesTran-MSFT answered

ASP.NET Framework MSGraph empty GetAccountsAsync()

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

dotnet-aspnet-webformsazure-ad-msal
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Preishuber-1903
Thank you for your post!

So I can gain a better understanding of your issue, can you share:

  • The full error message or exception that you're receiving?

  • Any screenshots or documentation that you're following?

  • More details on what you're trying to do?

Any additional information would be greatly appreciated.


If you have any other questions, please let me know.
Thank you for your time and patience throughout this issue.

0 Votes 0 ·

1) on acquiretoken silent
{"No account or login hint was passed to the AcquireTokenSilent call. "}

which make sense cause GetAccountsAsync is empty

i pushed the sample
https://github.com/hannespreishuber/ppcompany/tree/master/ppcompany

my goal is to write a walkthrough for Web Form developers.

0 Votes 0 ·

1) on acquiretoken silent
{"No account or login hint was passed to the AcquireTokenSilent call. "}

which make sense cause GetAccountsAsync is empty

pushed the sample
https://github.com/hannespreishuber/ppcompany/tree/master/ppcompany

my goal is to write a walkthrough for Web Form developers.

0 Votes 0 ·

@Preishuber-1903
Thank you for following up!

From your error message No account or login hint was passed to the AcquireTokenSilent call., I found a few related issues that might help point you in the right direction.

MsalUiRequiredException: No account or login hint was passed to the AcquireTokenSilent call:
GetAccountsAsync returns all the available accounts in the user token cache for the application. When you call GetAccountsAsync for the first time or if you the token cache is empty it can return empty accounts collection. In that case AcquireTokenSilent will throw the exception. Add try catch block around AcquireTokenSilent and if an expcetion is raised then call AcquireTokenInteractive.

 AuthenticationResult authResult;
 try
 {
     authResult = await PublicClientApp.AcquireTokenSilent(scopes, firstAccount)
                                           .ExecuteAsync();
 }
 catch (MsalUiRequiredException ex)
 {
     authResult = await PublicClientApp.AcquireTokenInteractive(scopes)
                               .ExecuteAsync();
 }


If you have any other questions, please let me know.
Thank you for your time and patience throughout this issue.

0 Votes 0 ·

this answere doenst help and reflect my question

as you can see - the code part acquieretokensilent is already within a try block






0 Votes 0 ·

1 Answer

JamesTran-MSFT avatar image
0 Votes"
JamesTran-MSFT answered

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.



image.png (71.7 KiB)
image.png (71.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.