启用代理身份验证正常的Windows应用程序 [英] Enable Proxy Authentication for normal windows application
问题描述
我公司的网络工程代理服务器上的身份验证(IE浏览器将提示窗口,用户名/密码,每次我试图访问任何网页)。
现在我有一些Windows应用程序试图访问网络(如WebPI / Visual Studio 2008中的RSS提要),但他们无法弹出身份验证窗口,他们无法与网络错误连接:
(407)代理身份验证
。 这里的例外是VS2008,第一次总是失败在启动时加载页面的RSS源,但是当我点击链接,它显示验证窗口和一切后,工作正常。
我的问题是:如何配置正常的Windows应用程序(通过的app.config / app.manifest文件)访问Web,以能够显示身份验证窗口或提供默认凭据
。
有关探索这个furthor,我已经创造了它试图检索算法的东西在谷歌和在控制台上显示的结果VS2008上一个控制台应用程序。 code:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Net;
使用System.IO;
命名空间WebAccess.Test
{
类节目
{
静态无效的主要(字串[] args)
{
Console.WriteLine(请输入检索算法标准:);
字符串标准=到Console.ReadLine();
字符串baseAddress =http://www.google.com/search?q=;
字符串输出=;
尝试
{
//创建的Web请求
HttpWebRequest的要求= WebRequest.Create(baseAddress +标准)为HttpWebRequest的;
//获取响应
使用(HttpWebResponse响应= request.GetResponse()作为HttpWebResponse)
{
//获取响应流
StreamReader的读者=新的StreamReader(response.GetResponseStream());
//控制台应用程序的输出
输出= reader.ReadToEnd();
}
Console.WriteLine(\ nResponse:\ñ\ N {0},输出);
}
赶上(例外前)
{
Console.WriteLine(\ n错误:\ñ\ N {0},ex.ToString());
}
}
}
}
在运行此,它给误差
输入检索算法标准:
拉利特
错误 :
System.Net.WebException:远程服务器返回错误:(407)代理认证介绍
tication必需。
在System.Net.HttpWebRequest.GetResponse()
在WebAccess.Test.Program.Main(字串[] args)在D:\ LK \文档\ VS.NET \ WebAccess的。
测试\ WebAccess.Test \的Program.cs:行26
preSS任意键继续。 。 。
解析自己:
创建了一个组件提供默认代理凭据
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Net;
命名空间ProxyCredProvider
{
公共类DefProxy:IWebProxy
{
公共ICredentials证书
{
{返回新的NetworkCredential(XXXX,XXXX); }
组 { }
}
公共乌里GetProxy(URI目的地)
{
返回新的URI(HTTP://xx.xx.xx.xx:8080 /);
}
公共BOOL IsBypassed(URI主机)
{
返回false;
}
}
}
安装到GAC。 并使用的app.config上述模块连接到任何exe文件试图访问网络。
< XML版本=1.0编码=UTF-8&GT?;
<结构>
< system.net>
< defaultProxy启用=真useDefaultCredentials =假>
<模块类型=ProxyCredProvider.DefProxy,ProxyCredProvider,版本= 1.0.0.0,文化=中性公钥= 5e3bf8f3a8a14cca/>
< / defaultProxy>
< /system.net>
< /结构>
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:
(407) Proxy Authentication Required
. 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.
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.
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:
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 . . .
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.
<?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>
这篇关于启用代理身份验证正常的Windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!