【问题标题】:Clear Credentials stored in Universal Windows App清除存储在通用 Windows 应用程序中的凭据
【发布时间】:2016-10-27 03:25:30
【问题描述】:

我在通用 Windows 平台 (UWP) 中使用 Windows.Web.Http.HttpClient。 URL 需要域凭据 (NTLM),因此 Windows 会为用户名和密码打开一个自定义弹出窗口。应用程序需要注销功能,但我找不到;找不到可以清除 UWP 存储的凭据的工作代码。

我尝试使用以下代码从 Windows.Security.Credentials.PasswordVault 清除凭据,但没有成功:

        var cred = new Windows.Security.Credentials.PasswordVault(); 
        var pwds = cred.RetrieveAll();
        foreach(var pwd in pwds)
        {
            pwd.RetrievePassword();                 
            cred.Remove(pwd);
        }

我也在清除 cookie,如下所示:

        var filter = new HttpBaseProtocolFilter();            
        var cookieManager = filter.CookieManager;
        HttpCookieCollection cookies = cookieManager.GetCookies(uri);            
        foreach (HttpCookie u in cookies)
        {
            cookieManager.DeleteCookie(u);
        }

有什么建议吗?

【问题讨论】:

  • 之后可能会清除cookies吗?
  • @AlexejSommer 我已经在清除 cookie,因为已编辑问题。

标签: c# win-universal-app uwp windows-10-universal windows-10-mobile


【解决方案1】:

这在 Windows 10 中不可用,但将在周年更新中:

var filter = new HttpBaseProtocolFilter();
filter.ClearAuthenticationCache();

您可以在 the MSDN page 上看到更多信息,如果您有一个 14295 之后的 Insider Preview 版本/SDK,您应该能够对其进行测试。

【讨论】:

    【解决方案2】:

    请看:

    https://docs.microsoft.com/en-us/windows/uwp/security/credential-locker#deleting-user-credentials

    描述了删除凭证的功能。

    您使用的方法public IReadOnlyList<PasswordCredential> RetrieveAll() 似乎返回了一个只读集合。因此它的值不能被删除。

    尝试访问凭据,例如与public PasswordCredential Retrieve(String resource, String userName)。非只读的返回类型应该使您能够使用删除方法。

    如果您想删除特定资源名称的所有凭据,即使在旧版 Windows 10 中也可以使用此解决方法:

    private void RemoveAllCredentials(PasswordVault passwordVault)
        {
            //Get all credentials.
            List<PasswordCredential> passwordCredentials = new List<PasswordCredential>();
            var credentials = passwordVault.RetrieveAll();
            foreach (PasswordCredential credential in credentials)
            {
                if (credential.Resource.Equals("ResourceName"))
                {
                    passwordCredentials.Add(
                        passwordVault.Retrieve(credential.Resource, credential.UserName));
                }
            }
            foreach (PasswordCredential entry in passwordCredentials)
            {
                passwordVault.Remove(entry);
            }
        }
    

    【讨论】:

    • 似乎有效。我不确定 100% 肯定,但无论如何我没有收到任何错误。
    猜你喜欢
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多