传递NTLM身份验证代理

时间:2013-03-19 16:21:58

标签: .net vb.net proxy ntlm

很久以前,我在.NET Framework 2.0上使用VB开发了一个遗留的HTTP客户端应用程序。 最近我们的网络引入了一个新的代理服务器,VB应用程序开始满足“407 Proxy Authentication Required”错误。

代理需要NTLM身份验证,程序不考虑它。

在搜索了一些网络资源后,我编写了以下代码。

Dim proxy As New System.Net.WebProxy("http://my.proxy.com:81")
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials
proxy.UseDefaultCredentials = true
webreq.Proxy = proxy

但我仍然看到407错误。 用户正在登录Windows域。

我尝试了一些不同(但相似)的方法,但没有成功。

任何人都可以指出我可能缺少的东西吗?

是否有任何安全策略设置可能阻止此操作?

我可以联系网络管理员,但他不熟悉应用程序开发,我也不知道我应该问他什么。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我正在寻找解决方案。 也许它与安全相关:它们是否在Intranet安全区域? http://www.codinghorror.com/blog/2005/04/aspnet-ntlm-authentication---is-it-worth-it.html

我在http://bytes.com/topic/net/answers/544841-ntlm-authentication-how找到了这个 这是一个开始,但他们也遇到了问题。

public string SendRequest()
        // run the request and return a string response
        {
            string FinalResponse = "";
            string Cookie = "";

            NameValueCollection collHeader = new NameValueCollection();

            HttpWebResponse webresponse;

             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URI);

             request.KeepAlive = true;
            request.Timeout = 10000;
            request.Method = "POST";
            request.AllowAutoRedirect = false;
            request.Proxy = WebProxy.GetDefaultProxy();

            string addr = "http://" + ProxyServer + ":" + String.Format("{0:D}", ProxyPort);
            Uri u = new Uri(addr);

            CredentialCache wrCache = new CredentialCache();
            wrCache.Add(u, "Negotiate", System.Net.CredentialCache.DefaultNetworkCredentials);

            request.Proxy.Credentials = wrCache;

            try
            {
                byte[] bytes = Encoding.ASCII.GetBytes(Request);
                request.ContentLength = bytes.Length;

                Stream oStreamOut = request.GetRequestStream();
                oStreamOut.Write(bytes, 0, bytes.Length);
                oStreamOut.Close();

                webresponse = (HttpWebResponse)request.GetResponse();
                if (null == webresponse)
                {
                    FinalResponse = "No Response from " + URI;
                }
                else
                {
                    Encoding enc = System.Text.Encoding.GetEncoding(1252);
                    StreamReader rdr = new StreamReader(webresponse.GetResponseStream(),enc);
                    FinalResponse = rdr.ReadToEnd();
                }

            }//End of Try Block

            catch (WebException e)
            {
                // some kind of error..
                if (407 == (int)e.Status)
                {
                }

                throw CatchHttpExceptions(FinalResponse = e.Message);
            }
            catch (System.Exception e)
            {
                throw new Exception(FinalResponse = e.Message);
            }
            finally
            {
                // BaseHttp = null;
            }
            return FinalResponse;
        } //End of SendRequestTo method