2222using System . Linq ;
2323using System . Net . Http ;
2424using System . Net . Http . Headers ;
25+ using System . Threading . Tasks ;
2526using System . Web . Script . Serialization ;
2627using System . Windows ;
2728
@@ -68,7 +69,8 @@ protected override async void OnInitialized(EventArgs e)
6869 // get a token for the user without showing a UI.
6970 try
7071 {
71- result = await app . AcquireTokenSilentAsync ( Scopes , app . Users . FirstOrDefault ( ) ) ;
72+ var accounts = await app . GetAccountsAsync ( ) ;
73+ result = await app . AcquireTokenSilentAsync ( Scopes , accounts . FirstOrDefault ( ) ) ;
7274 // If we got here, a valid token is in the cache - or MSAL was able to get a new oen via refresh token.
7375 // Proceed to fetch the user's tasks from the TodoListService via the GetTodoList() method.
7476
@@ -105,7 +107,7 @@ public MainWindow()
105107 InitializeComponent ( ) ;
106108 }
107109
108- private async void GetTodoList ( )
110+ private async Task GetTodoList ( )
109111 {
110112
111113 // Get a token from MSAL, and attach
@@ -118,8 +120,8 @@ private async void GetTodoList()
118120 // Here, we try to get an access token to call the TodoListService
119121 // without invoking any UI prompt. AcquireTokenSilentAsync forces
120122 // MSAL to throw an exception if it cannot get a token silently.
121-
122- result = await app . AcquireTokenSilentAsync ( Scopes , app . Users . FirstOrDefault ( ) ) ;
123+ var accounts = await app . GetAccountsAsync ( ) ;
124+ result = await app . AcquireTokenSilentAsync ( Scopes , accounts . FirstOrDefault ( ) ) ;
123125 }
124126 catch ( MsalException ex )
125127 {
@@ -183,7 +185,8 @@ private async void AddTodoItem(object sender, RoutedEventArgs e)
183185 AuthenticationResult result = null ;
184186 try
185187 {
186- result = await app . AcquireTokenSilentAsync ( Scopes , app . Users . FirstOrDefault ( ) ) ;
188+ var accounts = await app . GetAccountsAsync ( ) ;
189+ result = await app . AcquireTokenSilentAsync ( Scopes , accounts . FirstOrDefault ( ) ) ;
187190 }
188191 catch ( MsalException ex )
189192 {
@@ -226,11 +229,13 @@ private async void AddTodoItem(object sender, RoutedEventArgs e)
226229 /// Clears the cache
227230 /// </summary>
228231 /// <param name="app"></param>
229- private void ClearCache ( IPublicClientApplication app )
232+ private async Task ClearCache ( IPublicClientApplication app )
230233 {
231- foreach ( IUser user in app . Users . ToArray ( ) )
234+ var accounts = await app . GetAccountsAsync ( ) ;
235+ while ( accounts . Any ( ) )
232236 {
233- app . Remove ( user ) ;
237+ await app . RemoveAsync ( accounts . First ( ) ) ;
238+ accounts = await app . GetAccountsAsync ( ) ;
234239 }
235240 }
236241 private async void SignIn ( object sender = null , RoutedEventArgs args = null )
@@ -245,7 +250,7 @@ private async void SignIn(object sender = null, RoutedEventArgs args = null)
245250 if ( SignInButton . Content . ToString ( ) == "Clear Cache" )
246251 {
247252 TodoList . ItemsSource = string . Empty ;
248- ClearCache ( app ) ;
253+ await ClearCache ( app ) ;
249254 SignInButton . Content = "Sign In" ;
250255 return ;
251256 }
0 commit comments