C# (407) 需要代理身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15710733/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:41:18  来源:igfitidea点击:

(407) Proxy Authentication Required

c#proxyproxy-authentication

提问by jlh3302

I know this has been asked many many times. I have read most all posts on here and other sites like this one.

我知道这已经被问过很多次了。我已经阅读了这里和其他类似网站上的大部分帖子。

http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/2931d21c-9ca8-4256-b213-915fad4c941b/

http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/2931d21c-9ca8-4256-b213-915fad4c941b/

With no avail. Here's the environment

无济于事。这里的环境

Windows Server 2008 R2 64bit Visual Studio 2008 .Net Framework 3.5

Windows Server 2008 R2 64 位 Visual Studio 2008 .Net Framework 3.5

Here is what I have tried

这是我尝试过的

I had the proxy authenticating using code

我使用代码进行了代理身份验证

WebRequest req = WebRequest.Create(requestUri + data);
req.Proxy = new System.Net.WebProxy(<ProxyURL>:<port>",true);
req.Proxy.Credentials = CredentialCache.DefaultCredentials;
WebResponse resp = req.GetResponse();

This worked, but seeing as it was slowing down the app I learned that I can edit the machine.config file which I did. It worked too!

这行得通,但看到它减慢了应用程序的速度,我了解到我可以编辑我所做的 machine.config 文件。它也起作用了!

    <system.net>
      <defaultProxy
      useDefaultCredentials="true">
      <proxy
        proxyaddress="<proxyURL>:<port>"
        bypassonlocal="True"/>
     </defaultProxy>
   </system.net>

At least for a day or 2. Then it began failing.

至少一两天。然后它开始失败。

I then edited it to this

然后我把它编辑成这个

    <system.net>
     <defaultProxy
       useDefaultCredentials="true">
       <proxy usesystemdefault="True"/>
       </defaultProxy>
    </system.net>

From my understanding this will use IE settings to connect to proxy but still does not work. I also tried tihs code

根据我的理解,这将使用 IE 设置连接到代理,但仍然不起作用。我也试过 tihs 代码

WebProxy proxy = new WebProxy(<proxy>:<port>);
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(requestUri + data), "BASIC", new NetworkCredential(<username>,<password>));
proxy.Credentials = myCache;
request.Proxy = proxy;
request.Method = "GET";

And this did not work.

这没有用。

Note: I can copy the machine.config file to my computer(Win XP) and run the .exe(without the proxy code) from there and it works fine.

注意:我可以将 machine.config 文件复制到我的计算机(Win XP)并从那里运行 .exe(没有代理代码),它工作正常。

Is there something different I need to do with a 64 bit OS? Also I can open IE8 on the server and access the URI just fine. the goal is to preauthenticate the proxy without having to supply a username password in the code.

我需要对 64 位操作系统做一些不同的事情吗?我也可以在服务器上打开 IE8 并访问 URI 就好了。目标是预先验证代理,而无需在代码中提供用户名密码。

采纳答案by David Moore

HttpWebRequest uses the default Internet Settings (IE) proxy anyway, so if it works fine from Internet Explorer on the server, it should be ok from your code as well (provided it's running under the same user account).

HttpWebRequest 无论如何都使用默认的 Internet 设置 (IE) 代理,因此如果它在服务器上的 Internet Explorer 中工作正常,那么从您的代码中也应该没问题(前提是它在同一用户帐户下运行)。

I would put machine.config back as it was.

我会把 machine.config 放回原样。

One thing I would check would be in IIS, is you can configure the Providers for the Windows Authentication applet. This should list NTLM and Kerberos as the providers in a list; I would switch them around and see if that makes a difference (e.g. if NTLM is top of the list, move Kerberos to the top). I'm sorry I can't give you the exact instructions as I don't have IIS on this machine.

我要检查的一件事是在 IIS 中,您可以为 Windows 身份验证小程序配置提供程序。这应该将 NTLM 和 Kerberos 列为列表中的提供程序;我会切换它们,看看这是否有所不同(例如,如果 NTLM 位于列表顶部,则将 Kerberos 移至顶部)。很抱歉我不能给你确切的说明,因为我在这台机器上没有 IIS。

If you're still struggling, I would recommend you run Fiddler on the server to capture the request and response flow for more clues.

如果您仍然在挣扎,我建议您在服务器上运行 Fiddler 来捕获请求和响应流以获得更多线索。

回答by anagnam

@David Moore is right. if IE is working fine when you browse manually then just add req.Proxy.Credentials = CredentialCache.DefaultCredentials;and it will work fine.

@大卫摩尔是对的。如果手动浏览时 IE 工作正常,那么只需添加req.Proxy.Credentials = CredentialCache.DefaultCredentials;它就会正常工作。

Heres a modified code taken from the MSDN which is working for me.

这是从 MSDN 中获取的修改后的代码,它对我有用。

using System;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string urlDemo = "http://en.wikipedia.org/wiki/Main_Page";
            // Create a request for the URL. 
            WebRequest request = WebRequest.Create(urlDemo);
            // If required by the server, set the proxy credentials.
            request.Proxy.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            Console.ReadLine();
            // Clean up the streams and the response.
            reader.Close();
            response.Close();

        }
    }
}

hope it helps ;-)

希望能帮助到你 ;-)