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

∥☆過路亽.° 提交于 2019-12-11 05:53:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!