HttpWebRequest Over Proxy using Windows Authentication

Hi All,

I have an application which allows LDAP authentication.  I have a check (below) to determine if I have internet access or not as I'm invoking some third party web services.

This is currently not working if the application is behind a proxy.  How do I configure my web request to go out over the proxy wire using the credentials of the person who has been authenticated?

Thanks in advanced.
Public Function fnChkInternetConn() As Boolean
        Dim objreq As System.Net.HttpWebRequest
        Dim objres As System.Net.HttpWebResponse
        Dim user As WindowsIdentity = WindowsIdentity.GetCurrent()
        Dim ic As System.Security.Principal.WindowsImpersonationContext = user.Impersonate()
        fnChkInternetConn = False
        Try



            objreq = HttpWebRequest.Create("http://www.google.com")

            ' throws error but something like this needed
            ' objreq.Proxy.Credentials = System.Web.HttpContext.Current



            objres = CType(objreq.GetResponse(), System.Net.HttpWebResponse)

            objreq.Abort()
            If objres.StatusCode = System.Net.HttpStatusCode.OK Then
                fnChkInternetConn = True
            End If
        Catch weberrt As System.Net.WebException
            fnChkInternetConn = False
        Catch except As Exception
            fnChkInternetConn = False
        Finally
            ic.Undo()
        End Try

    End Function

Open in new window

LVL 42
Kyle AbrahamsSenior .Net DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
' Assign the credentials of the logged in user or the user being impersonated.
myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials


Dim myProxy As New WebProxy()
myProxy.Credentials = CredentialCache.DefaultCredentials
0
MlandaTCommented:
so in your case:

objreq.Proxy.Credentials = CredentialCache.DefaultCredentials
0
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
Thanks for the reply:

Exception Message: Unable to connect to the remote server.
  System.Net.Sockets.SocketException: {"An invalid argument was supplied 74.125.226.178:80"}

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
(Error happening when sending the request.  Works if I'm not on a proxy.)

0
MlandaTCommented:
Dim mywebRequest As HttpWebRequest = TryCast(WebRequest.Create("http://www.google.com/"), HttpWebRequest)
     
'mywebRequest.Proxy = New WebProxy(proxyServerIP, proxyServerPort) 'to download a file from HTTPS
mywebRequest.Proxy = New Proxy("http://proxyServerIP:proxyServerPort")

mywebRequest.Proxy.Credentials = CredentialCache.DefaultCredentials
mywebRequest.Credentials = CredentialCache.DefaultCredentials

--------------

Can you maybe also post your current source after modifications if this does not work?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kyle AbrahamsSenior .Net DeveloperAuthor Commented:
Made one slight change as to use the system proxy (rather than a configuration issue).
 objreq.Proxy = HttpWebRequest.GetSystemWebProxy

thanks very much!

Public Function fnChkInternetConn() As Boolean
        Dim objreq As System.Net.HttpWebRequest
        Dim objres As System.Net.HttpWebResponse
        Dim user As WindowsIdentity = HttpContext.Current.User.Identity
        Dim ic As System.Security.Principal.WindowsImpersonationContext = user.Impersonate()
        fnChkInternetConn = False
        Try



            objreq = TryCast(WebRequest.Create("http://www.google.com/"), HttpWebRequest)

            objreq.Proxy = HttpWebRequest.GetSystemWebProxy
            objreq.Proxy.Credentials = CredentialCache.DefaultCredentials
            objreq.Credentials = CredentialCache.DefaultCredentials
            objres = CType(objreq.GetResponse(), System.Net.HttpWebResponse)
            objreq.Abort()
            If objres.StatusCode = System.Net.HttpStatusCode.OK Then
                fnChkInternetConn = True
            End If
        Catch weberrt As System.Net.WebException
            fnChkInternetConn = False
        Catch except As Exception
            fnChkInternetConn = False
        Finally
            ic.Undo()
        End Try

    End Function

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.