CommunityToolkit / CommunityToolkit/Graph-Controls

MsalProvider not handling token expiration properly

未关闭
#189 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
Area: Providers bug :bug:
主要语言
C#
星标
156
派生
38
PR 合并指标
30 天内没有已合并 PR

描述

## 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:

```csharp
protected async Task 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

```

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。