简体   繁体   中英

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

I'm struggling with this problem for a week. I have to use API with Digest authentication in Windows Store App, but while I'm using this code I get System.ArgumentNullException in this line of code:

HttpHandler.Credentials = credCache;

Here is rest of code:

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();

What am I doing wrong?

A quick fix to your issue is to use credCache.GetCredentials() instead of just credCache when assigning a value to HttpHandler. Credentials as such:

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");

This works without the ArgumentNullException .

Hope this helps.

Thanks, Sid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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