【问题标题】:How to do sign out from MSAL authentication.?如何从 MSAL 身份验证中注销。?
【发布时间】:2019-11-28 04:24:43
【问题描述】:

我正在使用 MSAL 进行身份验证以进行身份​​验证的 xamarin 表单。现在我想在用户点击退出后退出。为了退出,我写了下面的代码

 foreach (var user in await App.PCA.GetAccountsAsync())
   {
      await App.PCA.RemoveAsync(user);
   }

上面的代码执行没有任何问题,但是当我再次尝试登录时,它并没有要求输入密码。我正在使用依赖服务从应用程序中清除 cookie,例如

在 xamarin forms 项目中创建了一个界面

public interface IAuthentication
{
    void ClearAllCookies();
}

并且在提供接口实现的android项目中

 public void ClearAllCookies()
{
    CookieManager.Instance.RemoveSessionCookie();
    CookieManager.Instance.RemoveAllCookie();
}

我正在像下面那样进行身份验证

在 App.xamal.cs 文件中

    public static IPublicClientApplication PCA = null;
    public static string ClientID = "xxxxxxxxxxxxxxxxxxxx";
    public static string[] Scopes = { "User.Read" };

    public App()
    {   
        PCA = PublicClientApplicationBuilder.Create(ClientID)
            .WithRedirectUri($"msal{ClientID}://auth")
            .Build();
        InitializeComponent();
    }

    //On login button click
    AuthenticationResult authResult = null;
    IEnumerable<IAccount> accounts = await App.PCA.GetAccountsAsync();

    try
    {
        IAccount firstAccount = accounts.FirstOrDefault();
        authResult = await App.PCA.AcquireTokenSilent(App.Scopes, firstAccount)
                                          .ExecuteAsync();
    }
    catch (MsalUiRequiredException)
    {
        try
        {
            authResult = await App.PCA.AcquireTokenInteractive(App.Scopes)
                                      .WithLoginHint(EmailId) //Here I am passing Email Id
                                      .WithParentActivityOrWindow(App.ParentWindow)
                                      .ExecuteAsync();
        }
        catch (Exception ex2)
        {
            await DisplayAlert("Acquire token interactive failed. See exception message for details: ", ex2.Message, "Dismiss");
        }
    }
    if (authResult != null)
    {
        var content = await GetHttpContentWithTokenAsync(authResult.AccessToken);
        await Navigation.PushModalAsync(new Dashboard.PartnerDashboard(content));                        
    }
}

再次清除后不再要求输入密码。如果我卸载并重新安装该应用程序,它也不会要求输入密码。如何解决?

【问题讨论】:

  • 您使用的是什么版本的 MSAL 库?
  • @FreakyAli。 4.7版本
  • 我使用的是 4.4,我从来没有遇到过问题!
  • 我尝试了 4.4 和 4.3 但结果没有变化。
  • 嗯,当您说不要求输入密码时,在这种情况下,您有两个不同的用户名和密码页面?当我说页面时,我的意思是在 webview 中!!!

标签: xamarin xamarin.forms xamarin.android msal


【解决方案1】:

您可以尝试使用以下代码在 MSAL 中清除帐户吗?

public void ClearAllCookies(string authority)
{
  var authContext = new AuthenticationContext(authority);
  authContext.TokenCache.Clear();
  CookieManager.Instance.RemoveSessionCookie();
  CookieManager.Instance.RemoveAllCookie();
}

其中权限 = "https://login.windows.net/common";

在验证代码运行之前尝试以下代码,以删除旧帐户。

   var authContext = new AuthenticationContext(authority);
   if(authContext.TokenCache.ReadItems().Any())
   {
    authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority,false, null); 
   }

【讨论】:

  • 感谢您的回复,我尝试使用您的代码,但同样的问题正在重复。
  • 好的。你能告诉我你的验证码一次吗?我会检查的。
  • 我在问题中添加了验证码请通过。
  • 如果只有条件,它不会进入内部。
猜你喜欢
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 2015-05-25
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
  • 2011-06-03
  • 2010-11-07
相关资源
最近更新 更多