我正试图强制从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/。原始应用程序没有允许用户注销的方法,因此仅在令牌过期时才调用。
答案 0 :(得分: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);
}
}