我有一个概念证明 W8.1-app,它允许使用 ADAL 库通过 Azure Active Directory 对用户进行身份验证。
我已经完成了允许用户登录和访问我的资源的工作。但是,它应该能够允许用户注销,并允许其他用户在同一设备上登录。
我在SO和其他地方发现了关于类似问题的其他问题,但在 IOS 或 WPF 应用程序中。在那里,他们建议<AuthenticationContext>.TokenCache.Clear()
使用以下调用调用并清除 cookie:
private void ClearCookies()
{
const int INTERNET_OPTION_END_BROWSER_SESSION = 42;
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
}
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
两者都不起作用(即使一起使用也不行)。打电话时
AuthenticationResult ar = await authContext.AcquireTokenAsync("https://xxx", "yyyy", new Uri("ms-app://callback/")
, new AuthorizationParameters(PromptBehavior.Auto, true));
如果我使用 PromptBehavior.Always,用户确实必须始终验证自己,但它不使用缓存。
关于如何使令牌缓存无效的任何想法?
提前致谢
编辑:已解决
感谢 vibronet,我能够通过执行以下操作成功注销用户:
authContext.TokenCache.Clear();
string requestUrl = "https://login.windows.net/common/oauth2/logout";
Task.Run(async () =>
{
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
var response = await client.SendAsync(request);
});
编辑2
您还可以通过将“common”替换为租户的别名来使用特定于您的应用程序/租户的 url,如下所示:
string tenantAlias = "TenantAlias.onmicrosoft.com";
string requestUrl = string.Format("https://login.windows.net/{0}/oauth2/logout", tenantAlias);