private async void SignIn(object sender = null, RoutedEventArgs args = null) { var accounts = (await _app.GetAccountsAsync()).ToList(); // If there is already a token in the cache, clear the cache and update the label on the button. if (SignInButton.Content.ToString() == clearCacheString) { TodoList.ItemsSource = string.Empty; FileCache.Clear(); while (accounts.Any()) { await _app.RemoveAsync(accounts.First()); accounts = (await _app.GetAccountsAsync()).ToList(); } // Also clear cookies from the browser control. SignInButton.Content = signInString; UserName.Content = Properties.Resources.UserNotSignedIn; return; } // // Get an access token to call the To Do list service. // AuthenticationResult result = null; try { // Force a sign-in (PromptBehavior.Always), as the ADAL web browser might contain cookies for the current user, and using .Auto // would re-sign-in the same user result = await _app.AcquireTokenInteractive(scopes).ExecuteAsync(); SignInButton.Content = clearCacheString; SetUserName(result.Account); GetTodoList(); } catch (MsalException ex) { if (ex.ErrorCode == "access_denied") { // The user canceled sign in, take no action. } else { // An unexpected error occurred. string message = ex.Message; if (ex.InnerException != null) { message += "Error Code: " + ex.ErrorCode + "Inner Exception : " + ex.InnerException.Message; } MessageBox.Show(message); } UserName.Content = Properties.Resources.UserNotSignedIn; return; } }