【问题标题】:Azure Logout AsyncAzure 注销异步
【发布时间】:2018-09-11 09:52:51
【问题描述】:

我正在尝试从 azure 服务器强制注销。如果您单击注销然后单击登录,则不会提示您输入用户名/密码,它只会让您直接重新登录。

我正在编写一个连接到 Azure Web 服务器的 UWP 应用程序。如果相关,您可以看到,当您注销时,它会从 Windows 凭据管理器中删除引用,并在您单击登录后立即恢复它。

我的问题本质上是,我还需要删除什么才能阻止应用检索以前的凭据而不是提示新用户可以登录?

    public async Task LogoutAsync()
    {
        if (Client.CurrentUser == null || Client.CurrentUser.MobileServiceAuthenticationToken == null)
            return;


        // Invalidate the token on the mobile backend
        var authUri = new Uri($"{Client.MobileAppUri}/.auth/logout");
        using (var httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Add("X-ZUMO-AUTH", Client.CurrentUser.MobileServiceAuthenticationToken);
            await httpClient.GetAsync(authUri);
        }


        // Remove the token from the cache
        _loginProvider.RemoveTokenFromSecureStore();

        // Remove the token from the MobileServiceClient
        await Client.LogoutAsync();
    }

    public async Task<MobileServiceUser> LoginAsync()
    {

        Client.CurrentUser = _loginProvider.RetrieveTokenFromSecureStore();

        if (Client.CurrentUser != null && !IsTokenExpired(Client.CurrentUser.MobileServiceAuthenticationToken))
        {
            // User has previously been authenticated, return current authenticated user
            return Client.CurrentUser;
        }

        if (Client.CurrentUser != null && IsTokenExpired(Client.CurrentUser.MobileServiceAuthenticationToken))
        {
            // Token is expired so perform a Logout
            await LogoutAsync();
        }

        // We need to ask for credentials at this point
        await _loginProvider.LoginAsync(Client).ConfigureAwait(false);
        if (Client.CurrentUser != null)
        {
            // We were able to successfully log in, store token for authenticated user
            _loginProvider.StoreTokenInSecureStore(Client.CurrentUser);
        }
        return Client.CurrentUser;
    }

我应该补充一点,代码(我没有编写原始程序)似乎基于此链接https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/。原始应用程序没有允许用户注销的方法,因此仅在令牌过期时才调用此方法。

【问题讨论】:

  • RemoveTokenFromSecureStore方法不行吗?
  • 它确实有效并且确实删除了令牌,当您重新运行程序时令牌会自动恢复。

标签: c# azure asynchronous uwp


【解决方案1】:

通常情况下,在 SO 上形成问题会导致我回答自己的问题。

它为您提供的登录屏幕包括一个保存 cookie 的 web 视图。强制清除这些已经解决了我的问题。我还清除了令牌缓存,尽管我认为这并不能真正解决所有情况下的问题。

如果我在使后端令牌无效之前调用此函数,那么它会按预期工作,并且在单击登录时会提示我输入用户名和密码。

    public void RemoveAuthenticationInfo()
    {
        var authContext = new AuthenticationContext(Authority);
        authContext.TokenCache.Clear();

        Windows.Web.Http.Filters.HttpBaseProtocolFilter myFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
        var cookieManager = myFilter.CookieManager;
        HttpCookieCollection myCookieJar = cookieManager.GetCookies(new Uri(Authority));
        foreach (HttpCookie cookie in myCookieJar)
        {
            cookieManager.DeleteCookie(cookie);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 2020-11-27
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 2021-10-04
    相关资源
    最近更新 更多