'MSAL Auto logout functionality in Xamarin

I am working on Xamarin app that uses MSAL library to authenticate to Web API that is secured by Azure AD. I am trying to implement automatic logout after certain period of time. The Sign In and Sign out functionality works but the auto-logout doesn't work. When I am trying to call the GetAccountAsync() method _pca.GetAccountsAsync(); the app crash.

 public async Task<bool> SignOutAsync()
    {
        try
        {
            var accounts = await _pca.GetAccountsAsync();

            // Go through all accounts and remove them.
            while (accounts.Any())
            {
                await _pca.RemoveAsync(accounts.FirstOrDefault());
                accounts = await _pca.GetAccountsAsync();
            }

            // Clear our access token from secure storage.
            SecureStorage.Remove("AccessToken");

            return true;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.ToString());
            return false;
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source