c# - HTTP 407(需要代理身份验证)

标签 c# http authentication httpwebrequest

我有一些关于 HTTP 身份验证的基本问题

1) 客户端如何知道服务器的 HTTP 身份验证类型(基本/摘要/NTLM)?
这可以在 HTTP 服务器端配置吗?

My Answer: Server will be set with which authentication type it has to perform with the client. So our client API(in C# HttpWebRequest API) will automatically take care of it. Best use of Wireshrk with applying HTTP filter; you will get source and Destination IP at Internet Protocol layer. And src and dest port at Transmission control protocol and athentication type at http layer.

2) 如果我在客户端和服务器之间放置 squid linux 代理;我的客户端代码是否还需要了解代理的身份验证类型?或者身份验证类型只与端 HTTP 服务器有关?

My Answer: If squid proxy is placed in between Client and Server; it wont use HTTP authentication. It may use a) DB: Uses a SQL database b) LDAP: Uses the Lightweight Directory Access Protocol. c) RADIUS: Uses a RADIUS server for login validation. and etc.. So we have to mention proxy authentication credentials in HTTP Headers.

3) 使用 WireShark 发现 Browser 到 Server 有 3 个请求来完成单个请求。

a) Browser sends a request without any authentication credentials; So server responded with 401 along with relam and nonce.

WWW-Authenticate: Digest realm="realm", qop="auth", nonce="MTM1OTYyMzkyNDU4MzpiOWM0OWY0NmMzMzZlMThkMDJhMzRhYmU5NjgwNjkxYQ=="\r\n <BR>

b) The second time Browser sends request with credentials, relam, nonce, cnonce; but still server responded with 401;

WWW-Authenticate: Digest realm="realm", qop="auth", nonce="MTM1OTYyMzk0OTAyMTo3Njk3MDNhZTllZDQyYzQ5MGUxYzI5MWY2MGU5ZDg0Yw==", stale="true"\r\n

c) The third time Browser send the same request with same credentials, relam, nonce, cnonce. This time Server sends 200 ok.

我的问题是在浏览器第二次和第三次发送相同的请求;为什么服务器第二次失败而第三次成功。这是因为我的服务器实现吗? (我有带有 SPRING 安全过滤器的 REST Java 服务器)。

我有 C# HTTP 客户端;

where in the first time HttpWebRequest is sent without credentials though the System.Net.NetworkCredentials is set; so clinet got 407 with relam, and nonce.
The second time HttpWebRequest is scuccess. There is no third request from this client like browser.

为什么浏览器和 C# 客户端之间存在这种差异?

My Answer: I am still don't know what is happening here: Q3.

4) 现在我面临的真正问题是当 SQUID LINUX PROXY 进入我们的客户端和 HTTP 服务器之间时,浏览器进行了相同的三个请求身份验证并成功了。但是,C# HttpWebRequest 在第二次请求时失败 (401) 并到达缓存(异常){} block 并且没有第三次尝试。

请问有人能给我解释一下当 PROXY SERVER 介于两者之间时如何在 C# 客户端中解决这个问题吗?

下面的代码正在执行 GET 请求。

  HttpWebRequest request = WebRequest.Create("url") as HttpWebRequest;
  request.Credentials = new NetworkCredential(loginUserName, password);
  WebResponse response = request.GetResponse(); 

请注意,我们对代理的请求是通过 TCP 协议(protocol)而不是 HTTP 协议(protocol)发送的。然后从 PROXY 到 SERVER 通过 HTTP 协议(protocol)进行通信。 但是来自 Proxy 的 HTTP 请求在 HTTP header X-Forwarded-For 中包含有关我们客户端 ip 的信息。

以下是可能的解决方案

仅当您的代理需要任何身份验证时才需要这些解决方案,否则请忽略它。

解决方案 1:为我工作

request.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

解决方案 2:

  IWebProxy proxy = WebRequest.GetSystemWebProxy();
  proxy.Credentials = new NetworkCredential(UserName, UserPassword, UserDomain);
  request.Proxy = proxy;

Martin给出的解决方案3:

  var proxy = new WebProxy ("http://localhost:3128/");
  proxy.Credentials = new NetworkCredential (UserName, UserPassword, UserDomain);
  request.Proxy = proxy;

了解有关代理身份验证的更多信息,请访问 http://wiki.squid-cache.org/Features/Authentication

最佳答案

这是一个挑战/响应协议(protocol)。通常,客户端会发出不带任何身份验证 header 的初始请求(您可以设置 request.PreAuthenticate = true 以在第一个请求中发送凭据)。

然后,服务器返回它支持的身份验证方法列表。

如果用户未使用 CredentialsCache 显式指定身份验证方法,运行时将尝试所有方法,从最强到最弱。某些协议(protocol)(例如 NTLM)需要从客户端到服务器的多个请求。理论上,Digest 应该只使用一个,不知道为什么它会发送两次请求。

关于您的代理问题,有两种不同的身份验证:

  1. 目标 Web 服务器的身份验证,代理只是传递它,您不需要在客户端中使用任何特殊代码。
  2. 除此之外,代理本身也可能需要身份验证 - 这可能与目标 Web 服务器不同。

您指定这些使用

var proxy = new WebProxy ("http://localhost:3128/");
proxy.Credentials = new NetworkCredential ("username", "password");

然后

WebRequest.DefaultWebProxy = proxy;

request.Proxy = proxy;

如果您的代理服务器不使用任何身份验证,请不要在 WebProxy 上设置任何凭据。

如果您在使用代理服务器时无法进行身份验证,请使用 Wireshark 查看在三方(Web 服务器、代理、客户端)之间发送的实际请求。

关于c# - HTTP 407(需要代理身份验证),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14641492/

相关文章:

c# - "Can´t open socket or connection refused"与 .NET

http - Express.js HTTP 请求超时

Java认证与授权

c# - WPF:将文件拖放到整个窗口中(包括标题栏和窗口边框)

c# - 在 C# 中通过 "dynamic overload"进行单/双分派(dispatch)

c# - NServiceBus 发布/订阅

http - 按需转码摄像机流

html - 我应该在页面上使用一个表单还是为每个项目生成一个表单?

python - 我有两个 Django 网站。我如何判断用户是否已经在其中一个上进行了身份验证?

authentication - 在 Web 应用程序中处理 session 超时的最佳方法?