.net 我应该如何设置默认代理以使用默认凭据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/299940/
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 should I set the default proxy to use default credentials?
提问by Brian Genisio
The following code works for me:
以下代码对我有用:
var webProxy = WebProxy.GetDefaultProxy();
webProxy.UseDefaultCredentials = true;
WebRequest.DefaultWebProxy = webProxy;
Unfortunately, WebProxy.GetDefaultProxy()is deprecated. What else should I be doing?
不幸的是,WebProxy.GetDefaultProxy()已弃用。我还应该做什么?
(using app.config to set the defaultProxy settings is not allowed in my deployment)
(在我的部署中不允许使用 app.config 设置 defaultProxy 设置)
采纳答案by Martin Hollingsworth
From .NET 2.0 you shouldn't need to do this. If you do not explicitly set the Proxy property on a web request it uses the value of the static WebRequest.DefaultWebProxy. If you wanted to change the proxy being used by all subsequent WebRequests, you can set this static DefaultWebProxy property.
从 .NET 2.0 开始,您不需要这样做。如果您没有在 Web 请求上显式设置 Proxy 属性,它将使用静态 WebRequest.DefaultWebProxy 的值。如果要更改所有后续 WebRequest 使用的代理,可以设置此静态 DefaultWebProxy 属性。
The default behaviour of WebRequest.DefaultWebProxy is to use the same underlying settings as used by Internet Explorer.
WebRequest.DefaultWebProxy 的默认行为是使用与 Internet Explorer 相同的基础设置。
If you wanted to use different proxy settingsto the current user then you would need to code
如果您想对当前用户使用不同的代理设置,则需要编写代码
WebRequest webRequest = WebRequest.Create("http://stackoverflow.com/");
webRequest.Proxy = new WebProxy("http://proxyserver:80/",true);
or
或者
WebRequest.DefaultWebProxy = new WebProxy("http://proxyserver:80/",true);
You should also remember the object model for proxies includes the concept that the proxy can be different depending on the destination hostname. This can make things a bit confusing when debugging and checking the property of webRequest.Proxy. Call
您还应该记住代理的对象模型包括代理可以根据目标主机名而不同的概念。在调试和检查 webRequest.Proxy 的属性时,这可能会使事情变得有点混乱。称呼
webRequest.Proxy.GetProxy(new Uri("http://google.com.au"))to see the actual details of the proxy server that would be used.
webRequest.Proxy.GetProxy(new Uri("http://google.com.au"))查看将使用的代理服务器的实际详细信息。
There seems to be some debate about whether you can set webRequest.Proxyor WebRequest.DefaultWebProxy = nullto prevent the use of any proxy. This seems to work OK for me but you could set it to new DefaultProxy()with no parameters to get the required behaviour. Another thing to check is that if a proxy elementexists in your applications config file, the .NET Framework will NOTuse the proxy settings in Internet Explorer.
关于是否可以设置webRequest.Proxy或WebRequest.DefaultWebProxy = null阻止使用任何代理似乎存在一些争论。这对我来说似乎工作正常,但您可以将其设置为new DefaultProxy()不带参数以获得所需的行为。要检查的另一件事是,如果您的应用程序配置文件中存在代理元素,.NET Framework 将不会使用 Internet Explorer 中的代理设置。
The MSDN Magazine article Take the Burden Off Users with Automatic Configuration in .NETgives further details of what is happening under the hood.
MSDN 杂志文章通过 .NET 中的自动配置减轻用户负担,提供了有关幕后发生的事情的更多详细信息。
回答by Andrew Webb
For those who, unlike Brian Genisio, areable to set the contents of their application's config file:- don't do anything in code. Instead add this to your app.config / web.config.
对于那些谁,不像布莱恩Genisio,都可以设置自己的应用程序的配置文件中的内容: -不要做任何代码。而是将其添加到您的 app.config/web.config。
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
Really and truly the default for using the default credentials should be "true"; I've seen this issue confuse so many people - developers, users, IT guys.
确实,使用默认凭据的默认设置应该是“true”;我已经看到这个问题让很多人感到困惑——开发人员、用户、IT 人员。
For more info see here:- http://sticklebackplastic.com/post/2007/01/26/Poxy-proxies.aspx
有关更多信息,请参见此处:- http://sticklebackplastic.com/post/2007/01/26/Poxy-proxies.aspx
UPDATE: I've created this issue/idea for Microsoft to change the default of useDefaultCredentials from false to true so that this whole problem goes away and .NET apps "just work"; please vote it up if you agree:
http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2397357-fix-it-so-that-net-apps-can-access-http-thru-auth
更新:我为 Microsoft 创建了这个问题/想法,将 useDefaultCredentials 的默认值从 false 更改为 true,以便整个问题消失,.NET 应用程序“正常工作”;如果您同意,请投票:http:
//visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2397357-fix-it-so-that-net-apps-can-access-http-thru-授权
回答by Thariq Nugrohotomo
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
This will force the DefaultWebProxyto use default credentials, similar effect as done through UseDefaultCredentials = true.
这将强制DefaultWebProxy使用默认凭据,效果与通过UseDefaultCredentials = true.
Hence allnewly created WebRequestinstances will use default proxy which has been configured to use proxy's default credentials.
因此,所有新创建的WebRequest实例都将使用默认代理,该代理已配置为使用代理的默认凭据。
回答by André
You may use Reflection to set the UseDefaultCredentials-Property from Code to "true"
您可以使用反射将UseDefaultCredentials代码中的-Property设置为“true”
System.Reflection.PropertyInfo pInfo = System.Net.WebRequest.DefaultWebProxy.GetType().GetProperty("WebProxy",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
((System.Net.WebProxy)pInfo.GetValue(System.Net.WebRequest.DefaultWebProxy, null)).UseDefaultCredentials = true;
回答by Daniel Weber
Seems like in some newer Application the Configuration is different, as i've seen on this Question How to authenticate against a proxy when using the HttpClient class?
似乎在一些较新的应用程序中,配置有所不同,正如我在这个问题中看到的那样,使用 HttpClient 类时如何对代理进行身份验证?
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="True" />
</defaultProxy>
</system.net>
Also documented on https://msdn.microsoft.com/en-us/library/dkwyc043.aspx
回答by smack
This thread is old, but I just recently stumbled over the defaultProxy issue and maybe it helps others.
这个线程很旧,但我最近偶然发现了 defaultProxy 问题,也许它可以帮助其他人。
I used the config setting as Andrew suggested. When deploying it, my customer got an error saying, there weren't sufficient rights to set the configuration 'defaultProxy'.
我按照安德鲁的建议使用了配置设置。在部署它时,我的客户收到一条错误消息,说没有足够的权限来设置配置“defaultProxy”。
Not knowing why I do not have the right to set this configuration and what to do about it, I just removed it and it still worked. So it seems that in VS2013 this issue is fixed.
不知道为什么我无权设置此配置以及如何处理它,我只是将其删除并且它仍然有效。所以似乎在 VS2013 中这个问题是固定的。
And while we're at it:
当我们在做的时候:
WebRequest.DefaultWebProxy.Credentials = new NetworkCredential("ProxyUsername", "ProxyPassword");
uses the default proxy with your credentials. If you want to force not using a proxy just set the DefaultWebProxy to null (though I don't know if one wants that).
使用带有您的凭据的默认代理。如果您想强制不使用代理,只需将 DefaultWebProxy 设置为 null(尽管我不知道是否需要)。
回答by jyz
In my deployment I can't use app.config neither to embed what Andrew Webb suggested.
So I'm doing this:
在我的部署中,我不能使用 app.config 来嵌入 Andrew Webb 建议的内容。
所以我这样做:
IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
WebClient wc = new WebClient();
wc.UseDefaultCredentials = true;
wc.Proxy = proxy;
Just in case you want to check my IE settings:
以防万一你想检查我的 IE 设置:


回答by Jim Scott
This is the new suggested method.
这是新的建议方法。
WebRequest.GetSystemWebProxy();
回答by Rodrigo T.
Is need in some systems set null the Proxy proprerty:
在某些系统中是否需要将 Proxy 属性设置为 null:
Net.WebRequest.DefaultWebProxy.Credentials = System.Net.CredentialCache.DefaultCredentials Dim request As WebRequest = WebRequest.Create(sRemoteFileURL) request.Proxy = Nothing
Net.WebRequest.DefaultWebProxy.Credentials = System.Net.CredentialCache.DefaultCredentials Dim request As WebRequest = WebRequest.Create(sRemoteFileURL) request.Proxy = Nothing
It's a bug.
这是一个错误。

