在 C# 中创建的 Web 代理需要提示进行身份验证
Posted
技术标签:
【中文标题】在 C# 中创建的 Web 代理需要提示进行身份验证【英文标题】:Web Proxy Created in C# needing to prompt for authentication 【发布时间】:2011-03-12 21:16:50 【问题描述】:我编写了一个使用 TcpListener 的小型 Web 代理程序,它在特定端口上侦听 Web 调用。一旦收到调用,它就会创建一个套接字,获取调用的标头,将其转发到请求的网页上,读取响应并将其返回给执行请求的客户端。代理工作得很好,但现在我想通过隐含用户名来限制谁可以通过代理访问网络。如何将身份验证合并到我的代理中?
这是我的代码:
使用系统; 使用 System.Net; 使用 System.Net.Sockets; 使用 System.Text; 使用 System.IO; 使用 System.Threading; 使用 System.Windows.Forms; 使用 System.Collections.Generic; 使用 System.ComponentModel; 使用 System.Data; 使用 System.Drawing;
命名空间 WebProxy2 公共部分类Form1:表格 public bool proxyOn = false;
public Form1()
InitializeComponent();
private void Form1_Load(object sender, EventArgs e)
public void log(string logText)
Application.DoEvents();
textBox1.Text += logText + "\r\n";
StreamWriter sw = new StreamWriter(@"c:\proxyLog.txt",true);
sw.WriteLine(logText);
sw.Close();
Application.DoEvents();
class WebProxy2
Socket clientSocket;
Form1 proxyGui;
Byte[] read = new byte[1024];
Byte[] Buffer = null;
Encoding ASCII = Encoding.ASCII;
const string HTTP_VERSION = "HTTP/1.0";
const string CRLF = "\r\n";
Byte[] RecvBytes = new Byte[4096];
public WebProxy2(Socket socket, Form1 form)
this.clientSocket = socket;
this.proxyGui = form;
public void run()
Application.DoEvents();
String clientmessage = " ", sURL = " ";
int bytes = readmessage(read, ref clientSocket, ref clientmessage);
if (bytes == 0)
return;
int index1 = clientmessage.IndexOf(' ');
int index2 = clientmessage.IndexOf(' ', index1 + 1);
if ((index1 == -1) || (index2 == -1))
throw new IOException();
proxyGui.log("Connecting to Site: " + clientmessage.Substring(index1 + 1, index2 - index1));
proxyGui.log("Connection from " + clientSocket.RemoteEndPoint);
string part1 = clientmessage.Substring(index1 + 1, index2 - index1);
int index3 = part1.IndexOf('/', index1 + 8);
int index4 = part1.IndexOf(' ', index1 + 8);
int index5 = index4 - index3;
sURL = part1.Substring(index1 + 4, (part1.Length - index5) - 8);
try
IPHostEntry IPHost = Dns.Resolve(sURL);
proxyGui.log("Request resolved: " + IPHost.HostName);
string[] aliases = IPHost.Aliases;
IPAddress[] address = IPHost.AddressList;
proxyGui.log(address[0].ToString());
IPEndPoint sEndpoint = new IPEndPoint(address[0], 80);
Socket IPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPsocket.Connect(sEndpoint);
if (IPsocket.Connected)
proxyGui.log("Socket connect OK");
string GET = clientmessage;
Byte[] ByteGet = ASCII.GetBytes(GET);
IPsocket.Send(ByteGet, ByteGet.Length, 0);
Int32 rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
proxyGui.log("Recieved " + rBytes);
//Buffer = RecvBytes;
String strRetPage = null;
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes);
while (rBytes > 0)
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes);
IPsocket.Shutdown(SocketShutdown.Both);
IPsocket.Close();
sendmessage(clientSocket, strRetPage);
catch (Exception exc2)
proxyGui.log(exc2.ToString());
private int readmessage(byte[] ByteArray, ref Socket s, ref String clientmessage)
int bytes = s.Receive(ByteArray, 1024, 0);
string messagefromclient = Encoding.ASCII.GetString(ByteArray);
clientmessage = (String)messagefromclient;
return bytes;
private void sendmessage(Socket s, string message)
Buffer = new Byte[message.Length + 1];
int length = ASCII.GetBytes(message, 0, message.Length, Buffer, 0);
s.Send(Buffer, length, 0);
const int port = 8080;
TcpListener tcplistener = new TcpListener(port);
private void btnProxy_Click_1(object sender, EventArgs e)
proxyOn = true;
log("Listening on port " + port + " " + DateTime.Now.ToString());
tcplistener.Start();
while (proxyOn)
Application.DoEvents();
if (tcplistener.Pending())
Socket socket = tcplistener.AcceptSocket();
WebProxy2 webproxy = new WebProxy2(socket, this);
webproxy.run();
Application.DoEvents();
private void btnStop_Click_1(object sender, EventArgs e)
Application.DoEvents();
proxyOn = false;
tcplistener.Stop();
log("Proxy stopped !!! " + DateTime.Now.ToString());
Application.DoEvents();
【问题讨论】:
【参考方案1】:尚不清楚您是否实现了自己的 HTTP 代理协议(或多或少是基于 Host
标头的简单 TCP 重定向)或者您是否使用标准 HTTP 代理协议。
如果您选择标准代理(这可能更好,除非您有充分的理由不这样做),您应该能够使用 Proxy-Authenticate
、Proxy-Authorization
和 Proxy-Authentication-Info
标头而不是 @例如,987654326@、Authorization
和 Authentication-Info
(分别)用于 HTTP Basic 或 Digest 身份验证。
【讨论】:
请查看我的代码以了解我在做什么。请告诉我更多您建议的标准代理 您是否至少在浏览器中将其用作常规 HTTP 代理(这样配置)? 插入这些标题有效。在msdn.microsoft.com/en-us/library/cc237546(PROT.13).aspx 找到了该过程的示例。非常感谢以上是关于在 C# 中创建的 Web 代理需要提示进行身份验证的主要内容,如果未能解决你的问题,请参考以下文章