Authentication 如何以xamarin表单从microsoft注销?

Authentication 如何以xamarin表单从microsoft注销?,authentication,xamarin,xamarin.forms,xamarin.android,forms-authentication,Authentication,Xamarin,Xamarin.forms,Xamarin.android,Forms Authentication,我正在研究xamarin表格。其中,通过Microsoft使用AuthenticationContext登录。单击“登录”按钮后,它将重定向到Microsoft登录页面。登录正常。一旦我从用户登入登出按钮点击用户应该登出,如何做登出没有按钮点击 使用以下代码,我可以登录 public Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string re

我正在研究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;
        }
    }
公共任务身份验证(字符串权限、字符串资源、字符串clientId、字符串返回URI)
{
尝试
{
var authContext=新的AuthenticationContext(授权);
if(authContext.TokenCache.ReadItems().Any())
{
authContext=新的AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
}
var uri=新uri(returnUri);
var platformParams=新的PlatformParameters((活动)Forms.Context);
var authResult=authContext.AcquireTokenAsync(资源、客户端ID、uri、平台参数);
返回结果;
}
捕获(例外情况除外)
{
崩溃。跟踪错误(ex);
返回null;
}
}

请帮助我如何注销?

您可以通过清除令牌缓存来注销,然后退出应用程序,或者将应用程序主页设置为登录页面

对于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();

您可以通过清除令牌缓存来注销,然后退出应用程序,或者将应用程序主页设置为登录页面

对于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();

正在清除访问令牌以获得注销

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 
   .
   .
   .
}

正在清除访问令牌以获得注销

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 
   .
   .
   .
}

嘿,Sagar,你的问题不清楚“如何在没有按钮点击的情况下注销?”为什么用户在没有按钮点击的情况下注销?你的意思是自动退出吗?您可以详细说明自动退出所需的条件。这里有一个文档供参考。嘿,Sagar,你的问题不清楚“如何在没有按钮点击的情况下注销?”为什么用户在没有按钮点击的情况下注销?你的意思是自动退出吗?您可以详细说明自动退出所需的条件。这里有一个文档供参考。是的,它很好用。我想知道如何传递一封有权威的提示邮件?例如,在我的应用程序中,我们首先要求用户输入电子邮件。一旦用户输入电子邮件,我们将在数据库中检查用户是否存在于我们的数据库中?如果是,我们将重定向到Microsoft登录页面。在那里,我们应该只要求输入密码,电子邮件Id应该根据输入框中的文本自动绑定。如何实现这一点?是的,它运行良好。我想知道如何传递一封有权威的提示邮件?例如,在我的应用程序中,我们首先要求用户输入电子邮件。一旦用户输入电子邮件,我们将在数据库中检查用户是否存在于我们的数据库中?如果是,我们将重定向到Microsoft登录页面。在那里,我们应该只要求输入密码,电子邮件Id应该根据输入框中的文本自动绑定。如何实现这一点?是的,它运行良好。我想知道如何传递一封有权威的提示邮件?例如,在我的应用程序中,我们首先要求用户输入电子邮件。一旦用户输入电子邮件,我们将在数据库中检查用户是否存在于我们的数据库中?如果是,我们将重定向到Microsoft登录页面。在那里,我们应该只要求输入密码,电子邮件Id应该根据输入框中的文本自动绑定。如何实现这一点?是的,它运行良好。我想知道如何传递一封有权威的提示邮件?例如,在我的应用程序中,我们首先要求用户输入电子邮件。一旦用户输入电子邮件,我们将在数据库中检查用户是否存在于我们的数据库中?如果是,我们将重定向到Microsoft登录页面。在那里,我们应该只要求输入密码,电子邮件Id应该根据输入框中的文本自动绑定。如何做到这一点?