iOS/MAUI调用MSAL AcquireTokenSilent遇AADB2C90080错误求助
问题:iOS实例中MSAL AcquireTokenSilent抛出AADB2C90080授权过期错误
仅在单个iOS部署实例中,调用MSAL的await ClientApplication.AcquireTokenSilent方法时触发错误:
AADB2C90080: The provided grant has expired. Please re-authenticate and try again.
调用该方法前,已成功执行以下交互式登录代码:
public async Task DoLogin(bool forceInteractive = false, CancellationToken token = default) { bool tryInteractive = false; if (!forceInteractive) { try { var accounts = (await ClientApplication.GetAccountsAsync(SignInSignOutPolicy)).ToList(); if (accounts.Any()) { var result = await ClientApplication.AcquireTokenSilent(new string[] { ApiScope }, accounts.First()).ExecuteAsync(); IsLoggedIn = !string.IsNullOrEmpty(result.AccessToken); tryInteractive = !IsLoggedIn; } else tryInteractive = true; } catch (MsalUiRequiredException) { tryInteractive = true; } catch (Exception ex) { await Alert.Handle(ex.Message).GetAwaiter(); } } else tryInteractive = true; if (tryInteractive) { try { var result = await ClientApplication.AcquireTokenInteractive(new string[] { ApiScope }) .WithPrompt(Prompt.SelectAccount).ExecuteAsync(token); IsLoggedIn = !string.IsNullOrEmpty(result.AccessToken); } catch (Exception ex) { await Alert.Handle(ex.Message).GetAwaiter(); } } }
出错的ProcessImage方法代码如下:
public async Task<RunesPostResponse?> ProcessImage(byte[] imageBytes, CancellationToken token = default) { try { RunesPostResponse? resp = null; var accounts = (await ClientApplication.GetAccountsAsync(SignInSignOutPolicy)).ToList(); var result = await ClientApplication.AcquireTokenSilent(new string[] { ApiScope }, accounts.First()).WithForceRefresh(true).ExecuteAsync(token); //exception on this line using (var client = Create()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); using (var content = new ByteArrayContent(imageBytes)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); var response = await client.PostAsync("process_image/", content, token); if (response.IsSuccessStatusCode) { var raw = await response.Content.ReadFromJsonAsync<RunesResponseRaw>(cancellationToken: token); if (raw != null) { resp = new RunesPostResponse(); resp.Annotations = raw.annotations; resp.Success = raw.success; resp.AnnotatedImage = Convert.FromBase64String(raw.annotatedImage); } } } } return resp; } catch(Exception ex) { throw; } }
曾怀疑是iOS钥匙串缓存问题,尝试执行以下代码但未解决:
private async void ReactiveContentPage_Loaded(object sender, EventArgs e) { #if IOS SecureStorage.Default.RemoveAll(); #endif }
请求协助排查并解决该授权过期错误。
内容的提问来源于stack exchange,提问作者LeeAdama
相关产品推荐
相关产品推荐

