繁体   English   中英

Windows应用商店中使用HttpClient(C#)的摘要式身份验证

[英]Digest authentication in Windows Store app using HttpClient (C#)

我在这个问题上挣扎了一个星期。 我必须在Windows Store App中将API与摘要身份验证一起使用,但是当我使用此代码时,在以下代码行中得到System.ArgumentNullException:

HttpHandler.Credentials = credCache;

这是其余的代码:

var credCache = new CredentialCache();
credCache.Add(new Uri("https://myserverIP/api"),"Digest",new NetworkCredential("mylogin", "mypassword") );
var HttpHandler = new HttpClientHandler();
HttpHandler.Credentials = credCache;
var httpClient = new HttpClient(HttpHandler);
var answer = await httpClient.GetAsync(new Uri("https://myserverIP/api/?function=someKindOfFunction"));
answer.EnsureSuccessStatusCode();

我究竟做错了什么?

将问题分配给HttpHandler时,快速解决此问题的方法是使用credCache.GetCredentials()而不是credCache 这样的凭证:

var credCache = new CredentialCache();
credCache.Add(new Uri("https://myserverIP/api"),"Digest",new NetworkCredential("mylogin", "mypassword") );
var HttpHandler = new HttpClientHandler();
HttpHandler.Credentials = credCache.GetCredential(new Uri("https://myserverIP/api"), "Digest");

这可以在没有ArgumentNullException情况下工作。

希望这可以帮助。

谢谢,席德

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM