简体   繁体   English

AuthenticationHeaderValue与NetworkCredential

[英]AuthenticationHeaderValue Vs NetworkCredential

I'm trying to write a client for either HTTP Post or HTTP Get using HttpClient. 我正在尝试使用HttpClient为HTTP Post或HTTP Get编写客户端。 When Googling around I come across these methods that set these authentication within the HttpClient object. 当谷歌搜索时,我遇到了这些在HttpClient对象中设置这些身份验证的方法。 One uses NetworkCredential while the other uses AuthenticationHeaderValue 一个使用NetworkCredential而另一个使用AuthenticationHeaderValue

HttpClient sClient;
HttpClientHandler sHandler = new HttpClientHandler();
sHandler.Credentials = new NetworkCredential("UserName", "Password");
sClient = new HttpClient(sHandler);

OR 要么

HttpClient sClient new HttpClient();
sClient.DefaultRequestHeaders.Authorization = 
  new AuthenticationHeaderValue("Basic",Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("UserName:Password")));

Reading on MSDN does not give me a distinct answer about the differences between them. 在MSDN上阅读并没有给我一个关于它们之间差异的明确答案。 Is this a case where both will do the same thing except how its authentication information is stored? 这是一种情况,除了如何存储其身份验证信息之外,两者都会做同样的事情吗? such as AuthenticationHeaderValue puts it in the header while the other doesn't? 如AuthenticationHeaderValue将其放在标题中,而另一个则没有? Is one better than the other in term of my use case or best practices ? 在我的用例或最佳实践方面,一个比另一个好吗?

The 2nd approach is more flexible in such way that you can specify a type of authentication (eg, anonymous, basic, window, certificate, etc) to use. 第二种方法更灵活,您可以指定要使用的身份验证类型(例如,匿名,基本,窗口,证书等)。

If your first approach doesn't work, try to specify the 3rd param on NetworkCredential, which is the domain name. 如果您的第一种方法不起作用,请尝试在NetworkCredential上指定第3个参数,即域名。

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

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