【问题标题】:How to logout from microsoft in xamarin forms?如何以 xamarin 形式从 Microsoft 注销?
【发布时间】:2019-06-18 07:53:29
【问题描述】:

我正在处理 xamarin 表单。登录是通过 Microsoft 使用 AuthenticationContext。单击登录按钮后,它将重定向到 Microsoft 登录页面。登录工作正常。一旦我从用户登录后点击注销按钮,用户应该注销,如何注销没有点击按钮?

使用以下代码我可以登录

public Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri)
    {
        try
        {
            var authContext = new AuthenticationContext(authority);
            if (authContext.TokenCache.ReadItems().Any())
            {
                authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
            }


            var uri = new Uri(returnUri);
            var platformParams = new PlatformParameters((Activity)Forms.Context);
            var authResult = authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
            return authResult;
        }
        catch (Exception ex)
        {
            Crashes.TrackError(ex);
            return null;
        }
    }

请帮助我如何退出?

【问题讨论】:

标签: authentication xamarin xamarin.forms xamarin.android forms-authentication


【解决方案1】:

Logout通过清空Tokencache,然后退出应用,或者将App MainPage设置为Login Page。

对于 iOS:

public async Task LogoutAsync()
        {
            var authContext = new AuthenticationContext(authority);
            if (authContext.TokenCache.ReadItems().Any())
            {
                authContext.TokenCache.Clear();
            }

            //In addition to clearing the token cache, you should also clear the cookies in the web view.
            //Otherwise, the session cookies come into play and that is why you are seeing the web view come up and disappear immediately.
            foreach (var cookie in NSHttpCookieStorage.SharedStorage.Cookies)
            {
                NSHttpCookieStorage.SharedStorage.DeleteCookie(cookie);
            }
        }

Android也一样,只是清除cookie的方法不同:

        CookieManager.Instance.RemoveAllCookie();

【讨论】:

  • 是的,它工作正常。我想知道如何通过权威的提示电子邮件?例如,在我的应用程序中,我们首先要求用户输入电子邮件。一旦用户输入电子邮件,我们将在数据库中检查该用户是否存在于我们的数据库中?如果是,我们将重定向到 Microsoft 登录页面。那里我们应该只要求输入密码,电子邮件 ID 应该根据输入框中的文本自动绑定。如何做到这一点?
【解决方案2】:

清除访问令牌以注销

Public static void Logout()
{
  AuthenticationContext authContext = new 
  AuthenticationContext(AuthenticationConstants.Authority);
  TokenCache tokenCache = ac.TokenCache;
  tokenCache.Clear();
}

在返回登录页面之前调用此方法。我的意思是在返回登录页面之前,您必须清除访问令牌

Like example
private void NavigateToLoginViewController()
{ 
    // call the above logout method here 
   .
   .
   .
}

【讨论】:

  • 是的,它工作正常。我想知道如何通过权威的提示电子邮件?例如,在我的应用程序中,我们首先要求用户输入电子邮件。一旦用户输入电子邮件,我们将在数据库中检查该用户是否存在于我们的数据库中?如果是,我们将重定向到 Microsoft 登录页面。那里我们应该只要求输入密码,电子邮件 ID 应该根据输入框中的文本自动绑定。如何做到这一点?
猜你喜欢
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
  • 2019-06-01
  • 2018-06-22
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多