如何确定(优雅地)在 C# winforms 应用程序中是否需要代理身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/490177/
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
How do I determine (elegantly) if proxy authentication is required in C# winforms app
提问by Tim Jarvis
My use case is this, I want to call out to a webservice and if I am behind a proxy server that requires authentication I want to just use the default credentials...
我的用例是这样的,我想调用一个网络服务,如果我在需要身份验证的代理服务器后面,我只想使用默认凭据......
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
Otherwise I'll just simply make the call, It would be very nice to determine if the auth is required up front, rather than handle the exception after I attempt to make the call.
否则,我将只是简单地拨打电话,确定是否需要预先进行身份验证会非常好,而不是在我尝试拨打电话后处理异常。
Ideas?
想法?
采纳答案by Sean Bright
回答by Conceptdev
It was only after I had first deployed my appthat I realised some users were behind firewalls... off to work to test it. Rather than do a test for a '407 authentication required' I just do the same Proxy
setup whether it might be needed or not...
直到我第一次部署我的应用程序后,我才意识到一些用户在防火墙后面......开始测试它。而不是对“需要407身份验证”进行测试,我只是进行相同的Proxy
设置,无论是否需要......
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri.AbsoluteUri);
//HACK: add proxy
IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
req.Proxy = proxy;
req.PreAuthenticate = true;
//HACK: end add proxy
req.AllowAutoRedirect = true;
req.MaximumAutomaticRedirections = 3;
req.UserAgent = "Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; DeepZoomPublisher.com)";
req.KeepAlive = true;
req.Timeout = 3 * 1000; // 3 seconds
I'm not sure what the relative advantages/disadvantages are (try{}catch{} without proxy first, versus just using the above), but this code now seems to work for me both at work (authenticating proxy) and at home (none).
我不确定相对的优势/劣势是什么(先尝试不使用代理{}catch{},而不是只使用上面的),但此代码现在似乎对我在工作(验证代理)和在家(没有任何)。
回答by Orion Edwards
It looks like if you leave the Proxy stuff alone, .NET should just use the IE proxy settings, which seems like the most "correct" way of dealing with proxies...
看起来如果你不理会代理的东西,.NET 应该只使用 IE 代理设置,这似乎是处理代理的最“正确”的方式......
回答by marc_s
If you want to check for the Proxy settings in IE, you could also peek into the registry: check the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings
branch of the registry tree - lots of options and settings there. Most notably: ProxyEnable
(a DWORD, 0 = no proxy, 1 = proxy enabled).
如果您想检查 IE 中的代理设置,您还可以查看注册表:检查HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings
注册表树的分支 - 那里有很多选项和设置。最值得注意的是:(ProxyEnable
一个 DWORD,0 = 无代理,1 = 启用代理)。
回答by Tim Jarvis
Actually, seems that this isn't an issue after all, Previously I was setting the auth like so...
实际上,似乎这毕竟不是问题,以前我是这样设置身份验证的......
WebProxy proxy = new WebProxy(@"http://<myProxyAddress>:8080");
proxy.Credentials = new NetworkCredential(<myUSerName>, <myPassword>, <myDomain>);
WebRequest.DefaultWebProxy = proxy;
This would be fine for when I was behind the proxy but throw an error when there was no proxy, so of course I expected the same error above as I was still just setting the same credentials, but you know what they say about assuming things....in fact there is no error at all with setting the default creds, all is sweet.
这对于当我在代理后面但在没有代理时抛出错误时会很好,所以当然我预计会出现上述相同的错误,因为我仍然只是设置相同的凭据,但您知道他们对假设的看法。 ...事实上,设置默认信用根本没有错误,一切都很美好。