【发布时间】: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