在 WebRequest 中强制进行基本身份验证
我正在集成将使用的网络服务 HTTP-POST 用于请求和检索数据。远程服务器 需要根据 RFC 2617 进行基本身份验证
我的身份验证尝试失败。
它失败的原因是,即使我将“NetworkCredential”对象附加到“HttpWebRequest”对象的“Credentials”属性,标头中也不会发送任何身份验证信息, 即使我设置“PreAuthenticate”= true。
我缺少什么?
//使用的块
NetworkCredential netCredential = new NetworkCredential(" uid", "pwd");
Uri uri = new Uri("http://address of services");
ICredentials credentials = netCredential.GetCredential(uri, "Basic");
objRegistration.Credentials = credentials;
objRegistration.PreAuthenticate = true;
I am integrating web service that will use an
HTTP-POST to request and retrieve data. The remote server
requires basic authentication as per RFC 2617
My attempts to authenticate are failing.
It fails in that, even though I attach a 'NetworkCredential' object to the 'Credentials' property of a 'HttpWebRequest' object, no authentication information is sent in the header,
even if I set 'PreAuthenticate' = true.
What am I missing?
//chunk used
NetworkCredential netCredential = new NetworkCredential(" uid", "pwd");
Uri uri = new Uri("http://address of services");
ICredentials credentials = netCredential.GetCredential(uri, "Basic");
objRegistration.Credentials = credentials;
objRegistration.PreAuthenticate = true;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚发现这个非常方便的小代码块< /a> 完全按照您的需要去做。它手动将授权标头添加到代码中,而不等待服务器的质询。
像这样使用它
I've just found this very handy little chunk of code to do exactly what you need. It adds the authorization header to the code manually without waiting for the server's challenge.
Use it like this
对塞缪尔·杰克接受的答案进行了一些改进。不应使用默认编码“ISO-8859-1”,如本答案中所述 我应该使用什么编码进行 HTTP 基本身份验证?
因此代码如下所示:
Improving a little bit on samuel-jack's accepted answer. Instead of using the default encoding "ISO-8859-1" should be used as mentioned in this answer What encoding should I use for HTTP Basic Authentication?
So the code would look like this:
我在寻找答案时发现了这个问题,给出的答案有效但不灵活,所以如果您想要更好的 .NET 方式来做到这一点。
您可以将“Basic”替换为“Digest”、“NTLM”或“Negotiate”,这样也可以将多种类型添加到缓存中。
I found this question while looking for the answer, the answer given works but is not flexible so if you would like a better .NET way of doing this.
You can replace "Basic" with "Digest", "NTLM" or "Negotiate" as well as this giving the ability to add multiple types to the cache as well.
这是我的 OAuth 解决方案。该值位于变量 json 中。
Here's my solution for OAuth. The value is in variable json.