如何使用HttpWebRequest.Credentials物业基本身份验证? [英] How to use HttpWebRequest.Credentials Property for Basic Authentication?

查看:457
本文介绍了如何使用HttpWebRequest.Credentials物业基本身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用WebRequest 证书属性发送基本认证头? 为什么没有授权头与发送,即使 preAuthenticate 设置为true的要求?

 的WebRequest请求=(HttpWebRequest的)WebRequest.Create(https://api.github.com/user);
request.Credentials =新的NetworkCredential(githubUsername,githubPassword);
请求preAuthenticate = TRUE。
变种响应= request.GetResponse();
 

解决方案

服务器应该发送一个HTTP 401未授权响应code的包含的一个WWW身份验证的HTTP标头。

  WWW身份验证:基本境界=榜样
 

在preAuthenticate属性只适用的的验证已经发生。从MSDN:

  

真正用后请求发送一个HTTP Authorization头   验证已经发生;否则为false。缺省值是   假的。

进行更深入的解释请参见其他的答案。

How can I use the Webrequest Credentials Property to send an basic authentication header? Why isn't the Authorization header send with the request even whenPreAuthenticate is set to true?

WebRequest request = (HttpWebRequest)WebRequest.Create("https://api.github.com/user");
request.Credentials = new NetworkCredential("githubUsername", "githubPassword");
request.PreAuthenticate = true;
var response = request.GetResponse();

解决方案

The server should send a HTTP 401 Not Authorized response code containing a WWW-Authenticate HTTP header.

WWW-Authenticate: Basic realm="example"

The PreAuthenticate property only works after authentication has taken place. From MSDN:

true to send an HTTP Authorization header with requests after authentication has taken place; otherwise, false. The default is false.

See other answers for more in depth explanation.

这篇关于如何使用HttpWebRequest.Credentials物业基本身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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