简体   繁体   English

如何使用HttpWebRequest.Credentials属性进行基本身份验证?

[英]How to use HttpWebRequest.Credentials Property for Basic Authentication?

How can I use the Webrequest Credentials Property to send an basic authentication header? 如何使用Webrequest Credentials属性发送基本身份验证标头? Why isn't the Authorization header send with the request even when PreAuthenticate is set to true? 即使PreAuthenticate设置为true,为什么Authorization标头不会随请求一起发送?

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 HTTP标头的HTTP 401 Not Authorized响应代码。

WWW-Authenticate: Basic realm="example"

The PreAuthenticate property only works after authentication has taken place. PreAuthenticate属性仅在进行身份验证后才有效。 From MSDN: 来自MSDN:

true to send an HTTP Authorization header with requests after authentication has taken place; 如果在进行身份验证后发送带有请求的HTTP Authorization标头,则为true;否则为true。 otherwise, false. 否则,错误。 The default is false. 默认值为false。

See other answers for more in depth explanation. 有关详细信息,请参阅其他答案。

I've done some additional research based on Måns Tånneryd `s answer. 我根据MånsTånneryd的回答做了一些额外的研究。 And the link he posted in his comment: PreAuthenticate Property of WebRequest - Problem . 他在评论中发表的链接: WebRequest的PreAuthenticate属性 - 问题

First of all as described in his link the HttpWebRequest.PreAuthenticate Property does NOT send the Authentication header PRE authentication, but pre sends it in following requests, after Authentication. 首先,如他的链接中所述, HttpWebRequest.PreAuthenticate属性不会发送身份验证标头PRE身份验证,但会在身份验证之后预先将其发送到以下请求中。 From MSDN: 来自MSDN:

true to send an HTTP Authorization header with requests after authentication has taken place; 如果在进行身份验证后发送带有请求的HTTP Authorization标头,则为true;否则为true。 otherwise, false. 否则,错误。 The default is false. 默认值为false。

So even with the PreAuthenticate property set to true, we still need an WWW-Authenticate challenge with a 401 Unauthorized before anything happens. 因此,即使将PreAuthenticate属性设置为true,我们仍然需要在发生任何事情之前使用401 Unauthorized进行WWW-Authenticate挑战。 Now if we try to authenticate against github with the following code: 现在,如果我们尝试使用以下代码对github进行身份验证:

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

var response = request.GetResponse();

An WebException will be thrown, because we don't get a WWW-Authenticate challenge. 将抛出WebException ,因为我们没有获得WWW-Authenticate挑战。 If we capture this in Fiddler we will get the following: 如果我们在Fiddler捕获这个,我们将获得以下内容:

在此输入图像描述

However if we try this, with the exact same code, against a website that does return a WWW-Authenticate challenge we will see the following in Fiddler: 但是,如果我们尝试使用完全相同的代码,针对确实返回WWW-Authenticate挑战的网站,我们将在Fiddler中看到以下内容:

在此输入图像描述在此输入图像描述

And the response will have the result as expected. 响应将得到预期的结果。

I can successfull run this code accessing another server that also requires both SSL and authentication. 我可以成功运行此代码访问另一个同时需要SSL和身份验证的服务器。 This server differs from the github in that the github returns a json result saying that it requires authentication and the other server returns a "classic" 401 html page. 这个服务器与github的不同之处在于github返回一个json结果,说它需要身份验证,而另一个服务器返回一个“经典”401 html页面。 Sniffing the network you can see that the .net code tries to do anonymous auth even if you do set preauth to true which I think is rather confusing. 嗅探网络你可以看到.net代码试图进行匿名身份验证,即使你将preauth设置为true,我认为这是相当混乱的。 However, upon receiving a regular 401-page it tries again, and this time with the auth info and everything works. 然而,在收到常规的401页后,它再次尝试,这次使用身份验证信息,一切正常。 It seems to me though as if .net reacts differently upon receiving the json version of a 401, not making a second try. 在我看来,虽然.net在收到401的json版本时反应不同,而不是第二次尝试。

I guess this is not the answer you are looking for but hopefully it sheds some more light on the situation. 我想这不是你正在寻找的答案,但希望它能更清楚地了解情况。

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

相关问题 未知重定向时为HttpWebRequest.Credentials建立一个CredentialCache - Building a CredentialCache for HttpWebRequest.Credentials when redirects are uknown 将 CredentialCache 对象分配给 HttpWebRequest.Credentials 在 Windows 通用应用程序中不起作用 - Assign CredentialCache object to a HttpWebRequest.Credentials in Windows Universal Apps don't work 使用HttpWebRequest进行基本身份验证 - Basic authentication with HttpWebRequest 使用基本身份验证的 HttpWebRequest - HttpWebRequest using Basic authentication HttpWebRequest不传递凭据简单表单身份验证 - HttpWebRequest not passing Credentials simple form authentication c#:对使用基本身份验证的 api 使用当前凭证 - c#: use current credentials for api that uses basic authentication 如何将 C# REST API HttpClient 用于基本 Z099FB995346F31C749F6E40C3FZ 身份验证和默认 ADF 凭据? - How do I use C# REST API HttpClient for both Basic header authentication and for default AD credentials? C# - 使用HttpWebRequest传递身份验证凭据 - C# - passing authentication credentials with HttpWebRequest 具有基本身份验证的HttpWebRequest失败,DefaultNetworkCredentials出现401错误 - HttpWebRequest with basic authentication fails with 401 error for DefaultNetworkCredentials 如何从标头中检索基本身份验证凭据? - How can I retrieve Basic Authentication credentials from the header?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM