为普通 Windows 应用程序启用代理身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2873292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable Proxy Authentication for normal windows application
提问by Lalit
my company's internet works on proxy server with Authentication(i.e. Browser prompts with window for username/password everytime i tried to access any web page).
我公司的互联网在带有身份验证的代理服务器上工作(即每次我尝试访问任何网页时,浏览器都会提示输入用户名/密码的窗口)。
Now i have some windows application which tries to access internet(like WebPI/Visual Studio 2008 for rss feeds), but as they are unable to popup the the authentication window, they are unable to connect with internet with error:
现在我有一些 Windows 应用程序尝试访问互联网(如 WebPI/Visual Studio 2008 for RSS feeds),但由于它们无法弹出身份验证窗口,因此无法连接互联网并出现错误:
(407) Proxy Authentication Required
(407) 需要代理身份验证
. Here the exception is VS2008, first time it always fails to load rss feeds on startup page, but when i click on the link, it shows authentication window and everything works fine after that.
. 这里的例外是 VS2008,第一次它总是无法在启动页面上加载 rss 提要,但是当我单击链接时,它显示身份验证窗口,之后一切正常。
my question is: How can i configure normal windows application(through app.config/app.manifest file) accessing web to able to show the authentication window or provide default credentials.
我的问题是:如何配置正常的 Windows 应用程序(通过 app.config/app.manifest 文件)访问 web 以能够显示身份验证窗口或提供默认凭据。
For explore this furthor, i have created one console application on VS2008 which tries to serach something on google and display the result on console. Code:
为了进一步探索,我在 VS2008 上创建了一个控制台应用程序,它尝试在 google 上搜索一些内容并在控制台上显示结果。代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace WebAccess.Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Serach Criteria:");
string criteria = Console.ReadLine();
string baseAddress = "http://www.google.com/search?q=";
string output = "";
try
{
// Create the web request
HttpWebRequest request = WebRequest.Create(baseAddress + criteria) as HttpWebRequest;
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Console application output
output = reader.ReadToEnd();
}
Console.WriteLine("\nResponse : \n\n{0}", output);
}
catch (Exception ex)
{
Console.WriteLine("\nError : \n\n{0}", ex.ToString());
}
}
}
}
When running this, It gives error
运行这个时,它给出错误
Enter Serach Criteria:
Lalit
Error :
System.Net.WebException: The remote server returned an error: (407) Proxy Authen
tication Required.
at System.Net.HttpWebRequest.GetResponse()
at WebAccess.Test.Program.Main(String[] args) in D:\LK\Docs\VS.NET\WebAccess.
Test\WebAccess.Test\Program.cs:line 26
Press any key to continue . . .
回答by Lalit
Resolve myself:
自己解决:
Created an assembly to provide default proxy credentials
创建了一个程序集以提供默认代理凭据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace ProxyCredProvider
{
public class DefProxy : IWebProxy
{
public ICredentials Credentials
{
get { return new NetworkCredential("xxxx", "xxxx"); }
set { }
}
public Uri GetProxy(Uri destination)
{
return new Uri("http://xx.xx.xx.xx:8080/");
}
public bool IsBypassed(Uri host)
{
return false;
}
}
}
Installed it to GAC. and use App.Config to attach the above module to any exe trying to access web.
将其安装到 GAC。并使用 App.Config 将上述模块附加到任何尝试访问网络的 exe 文件。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="false">
<module type="ProxyCredProvider.DefProxy, ProxyCredProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5e3bf8f3a8a14cca" />
</defaultProxy>
</system.net>
</configuration>
回答by stuartd
Edit:
编辑:
There are a couple of utilities which will tunnel any app through a proxy listed at http://kakku.wordpress.com/2007/11/18/proxify-any-application-tsocks-and-proxychains-force-any-program-to-communicate-through-a-socks-https-proxy-or-use-cascading-proxies/- look for the 'Windows Alternatives' links..
有几个实用程序可以通过http://kakku.wordpress.com/2007/11/18/proxify-any-application-tsocks-and-proxychains-force-any-program-上列出的代理隧道传输任何应用程序- to-communicate-through-a-socks-https-proxy-or-use-cascading-proxies/- 寻找“Windows Alternatives”链接..
回答by Charles Gargent
Use a WebProxy object, then use the app.config to get proxyurl and username. For the password you might enter it in the console or throw up a password box.
使用 WebProxy 对象,然后使用 app.config 获取 proxyurl 和用户名。对于密码,您可以在控制台中输入它或抛出一个密码框。
WebProxy proxy = new WebProxy(proxyurl);
proxy.Credentials = new NetworkCredential(username, password, domain);
request.Proxy = proxy;
EDIT
编辑
Sorry I misunderstood how about NTLMAPS
对不起,我误解了NTLMAPS