【问题标题】:Enable Proxy Authentication for normal windows application为普通 Windows 应用程序启用代理身份验证
【发布时间】:2010-05-20 11:41:26
【问题描述】:

我公司的互联网使用身份验证在代理服务器上运行(即,每次我尝试访问任何网页时,浏览器都会通过窗口提示输入用户名/密码)。

现在我有一些试图访问 Internet 的 Windows 应用程序(例如用于 RSS 源的 WebPI/Visual Studio 2008),但是由于它们无法弹出身份验证窗口,因此它们无法连接到 Internet 并出现错误:

(407) 需要代理身份验证

。 这里的例外是VS2008,第一次它总是无法在启动页面上加载RSS提要,但是当我点击链接时,它会显示身份验证窗口,之后一切正常。

我的问题是:如何配置正常的 Windows 应用程序(通过 app.config/app.manifest 文件)访问网络以显示身份验证窗口或提供默认凭据。

为了进一步探索,我在 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());
            }
        }
    }
}

运行时报错

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 . . .

【问题讨论】:

    标签: .net windows configuration proxy


    【解决方案1】:

    解决我自己:

    创建了一个程序集以提供默认代理凭据

    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;
            } 
    
        }
    }
    

    将其安装到 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>
    

    【讨论】:

      【解决方案2】:

      编辑:

      有几个实用程序可以通过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”链接..

      【讨论】:

      • 我没有所有 Windows 应用程序的源代码,我需要一种通过配置文件配置代理凭据的方法,或者只是提示输入凭据。
      【解决方案3】:

      使用 WebProxy 对象,然后使用 app.config 获取 proxyurl 和用户名。对于密码,您可以在控制台中输入它或抛出一个密码框。

      WebProxy proxy = new WebProxy(proxyurl);
      proxy.Credentials = new NetworkCredential(username, password, domain);
      request.Proxy = proxy;
      

      编辑

      对不起,我误解了NTLMAPS 怎么样

      【讨论】:

      • 我没有所有 Windows 应用程序的源代码,我只需要一种通过配置文件配置代理凭据的方法,或者只显示输入凭据的提示。
      • 您从 NTLMAPS 引用的内容。
      • 我已查看链接,但无法理解您的意思?
      猜你喜欢
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      相关资源
      最近更新 更多