MsalProvider not handling token expiration properly

Abierto
#189 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
csharp

Línea de trabajo

Comienza con MsalProvider y su método GetTokenWithScopesAsync; después, sigue el flujo de adquisición del token descrito en los pasos de reproducción. Reproduce una solicitud después de que expire el token y verifica que se reutilice una cuenta ya autenticada sin solicitar la selección de una cuenta.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Area: Providers bug :bug:

Describe the bug

My app periodically check for files in OneDrive and i've notices that if the app is kept opened long enough, the token will expire and the user will be prompt to select an account again.

I've manage to work around this issue by creating my own MsalProvider and changing the GetTokenWithScopesAsync as follows:

protected async Task<string> GetTokenWithScopesAsync(string[] scopes, bool silentOnly = false)
{
    await SemaphoreSlim.WaitAsync();

    try
    {
        AuthenticationResult authResult = null;
        try
        {
            var account = Account ?? (await Client.GetAccountsAsync()).FirstOrDefault();
            if (account != null)
            {
                authResult = await Client.AcquireTokenSilent(scopes, account).ExecuteAsync();
            }
        }
        catch (MsalUiRequiredException)
        {
           // CODE ADDED TO WORK AROUND THE EXPIRATION TOKEN ISSUE
            if (Account != null)
            {
                Account = null;
                SemaphoreSlim.Release();
                return await GetTokenWithScopesAsync(scopes, silentOnly);
            }
        }
        catch
        {
            // Unexpected exception
            // TODO: Send exception to a logger.
        }

        if (authResult == null && !silentOnly)
        {
            try
            {
                var paramBuilder = Client.AcquireTokenInteractive(scopes);

                if (Account != null)
                {
                    paramBuilder = paramBuilder.WithAccount(Account);
                }
                paramBuilder = paramBuilder.WithPrompt(Microsoft.Identity.Client.Prompt.NoPrompt);

                authResult = await paramBuilder.ExecuteAsync();
            }
            catch
            {
                // Unexpected exception
                // TODO: Send exception to a logger.
            }
        }

        Account = authResult?.Account;

        return authResult?.AccessToken;
    }
    finally
    {
        SemaphoreSlim.Release();
    }
}

I'm not submitting this as a PR cause i think that this is not the best way to handle this issue.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Open an app and make a request so that a token is generated
  2. Keep the app open until the token expires
  3. Try to make another request
  4. The app will prompt you to select an account again.

Expected behavior

I believe that once the user has authenticated the account selection window should not be prompt again.

Environment

Package Version(s): 7.1.1

Windows 11 Build Number:
- [x] Windows 11 (22000)

App min and target version:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [x] May 2019 Update (18362)
- [x] Windows 11 (Build 22000)

Device form factor:
- [x] Desktop
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT

Visual Studio 
- [ ] 2017 (version: )
- [ ] 2019 (version: ) 
- [ ] 2019 Preview (version: )
- [x] 2022

Lenguaje dominante
C#
Estrellas
156
Forks
37
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

  • Sin Dockerfile ni archivo de Docker Compose
  • Tiene una plantilla de pull request
  • Sin guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de CommunityToolkit/Graph-Controls

Todos los issues de CommunityToolkit/Graph-Controls

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.