Windows应用商店中使用HttpClient(C#)进行摘要式身份验证 [英] Digest authentication in Windows Store app using HttpClient (C#)

查看:125
本文介绍了Windows应用商店中使用HttpClient(C#)进行摘要式身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

以下是其余代码:

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

我在做什么错了?

推荐答案

解决此问题的快速方法是使用 credCache.GetCredentials()而不是仅 credCache 在为HttpHandler分配值时。凭据如下:

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

此功能无需 ArgumentNullException

希望这会有所帮助。

谢谢,
Sid

Thanks, Sid

这篇关于Windows应用商店中使用HttpClient(C#)进行摘要式身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆