xamarin.forms - MSAL 注销似乎没有清除缓存
问题描述
我们使用 MSAL 将 Xamarin 表单应用程序与 Azure 身份验证集成。当我们注销时,我们正在从 PCA 中删除帐户,并且代码执行时没有任何问题。但是在随后的登录中,它甚至无需输入凭据即可获得身份验证。它正在使用先前输入的凭据登录。看起来缓存没有被正确清除。
private async void AuthenticateUser()
{
App.Scopes = new string[] { "<client_id>" + "/.default" };
var redirectUri = "msal<clientId>" + "://auth";
if (Device.RuntimePlatform == Device.iOS)
{
App.PCA = PublicClientApplicationBuilder.Create("<client_id>")
.WithIosKeychainSecurityGroup("<package_name>")
.WithRedirectUri(redirectUri)
.Build();
}
else
{
App.PCA = PublicClientApplicationBuilder
.Create(CommonHelper.ClientId)
.WithRedirectUri(redirectUri)
.Build();
}
var accounts = await App.PCA.GetAccountsAsync();
var uid = new UserIdentifier("<user_name>", UserIdentifierType.OptionalDisplayableId);
AuthenticationResult authResult;
try
{
while (accounts.Any())
{
await App.PCA.RemoveAsync(accounts.First());
accounts = (await App.PCA.GetAccountsAsync()).ToList();
}
var firstAccount = accounts.FirstOrDefault();
authResult = await App.PCA.AcquireTokenSilent(App.Scopes, firstAccount)
.ExecuteAsync();
ProceedToLogin(authResult.AccessToken);
}
catch (MsalUiRequiredException mex)
{
try
{
authResult = await App.PCA.AcquireTokenInteractive(App.Scopes)
.WithParentActivityOrWindow(App.ParentWindow)
.WithLoginHint("<user_name>")
.WithUseEmbeddedWebView(true)
.ExecuteAsync();
ProceedToLogin(authResult.AccessToken);
}
catch (Exception ex)
{
Log(ex);
}
}
}
请在下面找到将在注销期间执行的代码。
public void Logout(string authority)
{
if (App.PCA == null)
{
App.Scopes = new string[] { "<client_id>" + "/.default" };
var redirectUri = "msal<azure_client_id>://auth";
App.PCA = PublicClientApplicationBuilder.Create("<client_id>")
.WithIosKeychainSecurityGroup("<package_name>")
.WithRedirectUri(redirectUri)
.Build();
}
var accounts = App.PCA.GetAccountsAsync().Result;
if (accounts != null)
{
while (accounts.Any())
{
App.PCA.RemoveAsync(accounts.First());
accounts = App.PCA.GetAccountsAsync().Result;
}
}
}
此外,我们尝试使用以下代码清除 cookie。它在较低版本中运行良好,但问题再次出现在 iOS 14.6 及更高版本中。
var cookieStorage = NSHttpCookieStorage.SharedStorage;
foreach (var cookie in cookieStorage.Cookies)
{
cookieStorage.DeleteCookie(cookie);
}
解决方案
据我从我自己在 .NET 中的 MSAL 实现中了解这一点,这按预期工作,我不确定这对 Xamarin 的影响如何。当您注销用户时,他们将不再针对您的应用程序进行身份验证,但将保留对 Microsoft 的身份验证。当您将未经身份验证的用户发送到 Microsoft 端点以重新登录到您的应用程序时(如您的屏幕截图所示),Microsoft 正确识别出他们仍然登录到他们的 Microsoft 帐户,但是该帐户未登录到您的应用程序。此时,Microsoft 提供了您看到的列表,您可以选择使用经过身份验证的帐户或选择其他帐户来登录您的应用程序。
在针对 MS 进行身份验证时,它们有两个级别,针对 MS 的身份验证和针对您的应用程序的身份验证。您的应用程序只能清除针对自身的身份验证,而不是允许用户保持登录到其他 MS 服务(Outlook 等)的 Microsoft。
推荐阅读
- xamarin - 有什么方法可以将 Application.Current.Properties 存储在数据库中,以便在多个设备之间共享这些?
- java - Play framework 2.6 Java 无法在新 Mac 上编译
- python - Python 应用迭代 linregress
- r - 如何在R中做嵌套饼图,其中外环数据是内环的子集?
- javascript - onclicking 一个内部 div 也 onclicks 外部 div
- javascript - VueJS 在嵌套的 v-for 循环中动态添加表单组件?
- aws-sdk - 如何估计 AWS Glue 和 Athena 的客户利用率
- angular - 角度找不到上传的图像
- python - 使用 Selenium 和 BeautifulSoup 通过提供表 ID Python3.7 不会返回特定表(返回空数组)
- html - 删除 ::after css 伪元素周围的边框/边距