通过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域。

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

谁能指出我可能错过了什么?

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

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

我也在寻找解决方案。也许这与安全有关:他们是否在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

最新更新