首页 > 解决方案 > 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);
}

标签: xamarin.formsmsal

解决方案


据我从我自己在 .NET 中的 MSAL 实现中了解这一点,这按预期工作,我不确定这对 Xamarin 的影响如何。当您注销用户时,他们将不再针对您的应用程序进行身份验证,但将保留对 Microsoft 的身份验证。当您将未经身份验证的用户发送到 Microsoft 端点以重新登录到您的应用程序时(如您的屏幕截图所示),Microsoft 正确识别出他们仍然登录到他们的 Microsoft 帐户,但是该帐户未登录到您的应用程序。此时,Microsoft 提供了您看到的列表,您可以选择使用经过身份验证的帐户或选择其他帐户来登录您的应用程序。

在针对 MS 进行身份验证时,它们有两个级别,针对 MS 的身份验证和针对您的应用程序的身份验证。您的应用程序只能清除针对自身的身份验证,而不是允许用户保持登录到其他 MS 服务(Outlook 等)的 Microsoft。


推荐阅读