【发布时间】:2015-06-09 11:59:45
【问题描述】:
我遇到了一个问题,即应用尝试使用不同的身份验证方法从同一服务器访问资源,这两种方法是:
- 凭据(NTLM、基本等)
- OAuth(不记名)
设置 HttpBaseProtocolFilter
HttpBaseProtocolFilter 设置为:
- 禁用缓存
- 禁用自动 UI 凭据请求弹出窗口
代码
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.NoCache;
filter.CacheControl.ReadBehavior = HttpCacheReadBehavior.MostRecent;
filter.AllowUI = false;
添加服务器凭据
如果资源需要凭据,那么我使用:
filter.ServerCredential = new PasswordCredential(
RequestUri.ToString(),
UserName,
Password);
HttpClient httpClient = new HttpClient(filter);
添加 OAuth 令牌
如果资源需要我使用的 Bearer 令牌:
HttpClient httpClient = new HttpClient(filter);
httpClient.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue("Bearer", token);
ServerCredential 为空
filter.ServerCredential = null
从服务器获取响应
using(httpClient)
{
using(HttpRequestMessage requestMessage = new HttpRequestMessage(new HttpMethod(method), RequestUri))
{
using(HttpResponseMessage response = await httpClient.SendRequestAsync(requestMessage))
{
// Do something with response
}
}
}
问题
如果 HttpClient 请求使用 ServerCredential 返回 200(OK),那么即使 Bearer 令牌无效且 filter.ServerCredential 为空,随后的每个 Bearer 请求也会返回 200(OK)。
看起来好像 filter.ServerCredential 已缓存,并且所有后续调用都使用缓存的凭据进行身份验证。
如果我想进行Bearer 身份验证,我必须重新启动应用程序。
如何删除、禁用或清除 Windows.Web.Http.HttpClient 的ServerCredential?
我尝试过的事情:
删除所有 cookie
var cookieManager = filter.CookieManager;
HttpCookieCollection myCookieJar = cookieManager.GetCookies(RequestUri);
foreach (HttpCookie cookie in myCookieJar)
{
cookieManager.DeleteCookie(cookie);
}
myCookieJar 为空。
PasswordCredentialPropertyStore 的东西
Windows.Security.Credentials.PasswordCredentialPropertyStore credentialPropertyStore = new Windows.Security.Credentials.PasswordCredentialPropertyStore();
credentialPropertyStore 为空。
与
PasswordCredentialPropertyStore的方法清除is reserved for internal use and is not intended to be used in your code.
有什么想法吗?
【问题讨论】:
-
在没有任何知识的情况下,我猜也许您可以在
using块中使用HttpClient并关闭每个不同的请求。当然,作为一种解决方法。 -
这实际上是我正在做的...... :(
-
很奇怪,即使在离开
using块之后仍然保留凭据。看起来它正在操作系统中的用户级别缓存 -
如果是windows 8.1,请求完成后是否在控制面板的凭据管理器中找到密码?您可以尝试清除此请求的 Uri 的密码凭据,
try { var credentials = _vault.FindAllByResource(PasswordVault); foreach(var passwordCredential in credentials) { _vault.Remove(passwordCredential); } } catch { // ignored } -
您是否尝试过使用
System.Net.Http?
标签: c# windows-phone-8.1 windows-8.1 httpclient win-universal-app