问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/30240556/digest-authentication-in-windows-store-app-using-httpclient-c