How can I get the IP address of the host Web Server into a Session variable?

  • Thread starter Thread starter John Kotuby
  • Start date Start date
J

John Kotuby

Hi all,

I have a situation where a Web Server at an install site does not currently
have a publically registered Domain Name associated with the site. This
requires that the a user access the site from a remote (outside the LAN)
browser using the IP address, as in http://192.35.78.234 .

Currently in my code I use the following syntax to obtain the Root Web path
which I then append as a prefix to Images and Links in external documents
(Excel files) so the recipient of an HTML Email or one of the spreadsheets
can both resolve the images or click on a spreadsheet link to view a page on
the site.

Dim strRootWeb As String = Me.Request.Headers.Item("Host").ToString
strRootWeb = "http://" + strRootWeb
Session.Add("RootWeb", strRootWeb)

Then for a working link in an external document I will do something like:

Public strRootWeb = Session("RootWeb")
strRootWeb & "/Common/PageView.aspx

But this does not work if the name of the Web Server is not publically
registered with DNS servers.

Thanks for any help on resolving the IP address of a Web Server root.
 
Thanks for the reply Mark,

I think that will give me the actual IP address of the Server box that the
Web Application is running on. That would work for me if the was directly
connected to the Internet.
However, in this situation, the Web Server is behind a firewall and being
accessed via NAT.

What I am really looking for is the Public IP Address that is used in the
request to get to the Router that then finds the Server behind the firewall.

I am thinking that it is contained somewhere in the HttpRequest object just
as the Me.Request.Headers.Item("Host").ToString returns the string for the
Root Web as requested by the client.

When the browser sends out a request for http://www.url.com that is
immediately converted by a public DNS server to an IP address. It is that
public IP address that I seek to grab, not matter what Web server is hosting
our application. I have been looking for the property that contains the IP
address but can't seem to find it in the documentation.

Thanks...
 
if the firewall (or load balancer) is doing nat translation (which it should
be), then its not available (as only the firewall knows it).

your best bet is to add to the ipaddress to the web.config as a setting

-- bruce (sqlwork.com)


John Kotuby said:
Thanks for the reply Mark,

I think that will give me the actual IP address of the Server box that the
Web Application is running on. That would work for me if the was directly
connected to the Internet.
However, in this situation, the Web Server is behind a firewall and being
accessed via NAT.

What I am really looking for is the Public IP Address that is used in the
request to get to the Router that then finds the Server behind the firewall.

I am thinking that it is contained somewhere in the HttpRequest object just
as the Me.Request.Headers.Item("Host").ToString returns the string for the
Root Web as requested by the client.

When the browser sends out a request for http://www.url.com that is
immediately converted by a public DNS server to an IP address. It is that
public IP address that I seek to grab, not matter what Web server is hosting
our application. I have been looking for the property that contains the IP
address but can't seem to find it in the documentation.

Thanks...


Mark Rae said:
Not sure if this is what you want or not but, based on the subject of your
post, you can get the IP address of the host web server into a Session
variable like this:

Session["HostIP"] = Request.ServerVariables["LOCAL_ADDR"].ToString();
 
Thanks guys, I was hoping to find the requested public IP address in the
Request object somewhere, but apparently it is not to be found.

However gathering the response from http://www.whatismyip.org/ might just
work after all.

I am running a workstation that is behind a Gateway Firewall device and it
uses NAT. When I click on the whatismyip link above I get the public IP
Address that I must use to connect to my workstation from home via RDP
(using a non-standard Port of course).

So if I am getting the Public IP of a NATed workstation, why wouldn't that
technique work for a NATed Web Server...it's just another box? In fact any
response from another public site can ONLY return the public IP address of a
NATed box. Brilliant!

I am going brain-dead today because I can't even recall how to get the
HttpResponse returned from something like: http://www.whatismyip.org/ in my
code-behind or maybe in the Global.asax itself.

I will resort to hard-coding the IP Address in web.config or global.asax if
necessary, as that is not too difficult, but I see possibly 30 or more
deployments that might need this sort of thing.

Thanks again...you guys are always helpful.
 
Well, I think I'm getting closer, but maybe just chasing my tail.
I converted your code example to VB and here is what is ending up in my
Session variable.

Error: blank User-Agent header is not allowed! Please ask the author of your
IP address checking client to specify their client's name in the User-Agent
header

Maybe it's something I failed to do when creating the WebClient?
From the same machine that I am using as the Web Server, still I get just
the correct IP address in my Web Browser and that is ALL that is being sent
back.
Why should the web browser be any different than the WebClient?
Oh well...It's 5 oclock on a Friday. I knew it had to be 5 oclock somehwere!
 
Try the following:

Private Sub MyIPaddressButton_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyIPaddressButton.Click
Dim strHostIP As String = String.Empty

Using objWebClient As New WebClient()

Using objStreamReader As StreamReader = New
StreamReader(objWebClient.OpenRead
("http://www.whatismyip.com/automation/n09230945.asp"))

strHostIP = objStreamReader.ReadToEnd()

objStreamReader.Close()

End Using

End Using

End Sub

Jamal

John Kotuby said:
Well, I think I'm getting closer, but maybe just chasing my tail.
I converted your code example to VB and here is what is ending up in my
Session variable.

Error: blank User-Agent header is not allowed! Please ask the author of
your IP address checking client to specify their client's name in the
User-Agent header

Maybe it's something I failed to do when creating the WebClient?
From the same machine that I am using as the Web Server, still I get just
the correct IP address in my Web Browser and that is ALL that is being
sent back.
Why should the web browser be any different than the WebClient?
Oh well...It's 5 oclock on a Friday. I knew it had to be 5 oclock
somehwere!
 
if the firewall is configured correctly, this will not work, as the webserver
will not be able to access an internet dns server (ports blocked), which is
required to translate the dns name to an ipaddress.

the firewall (say its f5) may have an api your code can call.

-- bruce (sqlwork.com)


Jamal said:
Try the following:

Private Sub MyIPaddressButton_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyIPaddressButton.Click
Dim strHostIP As String = String.Empty

Using objWebClient As New WebClient()

Using objStreamReader As StreamReader = New
StreamReader(objWebClient.OpenRead
("http://www.whatismyip.com/automation/n09230945.asp"))

strHostIP = objStreamReader.ReadToEnd()

objStreamReader.Close()

End Using

End Using

End Sub

Jamal
 
Mark,
That site actually worked without changing any of my code. Thank you.

Sure, why should anyone use VB when C# is far superior. I suppose I could
have placed your code in Script tags and designated the language as C#. The
rest of the project is written in VB so I though I would try to maintain
continuity. I actually have not tried mixing C# and VB in the same project
(primarily because I am not too familiar with C#).

Also I guess my firewall is not set up properly because other posters have
said this should not work. I am not sure what they are driving at, but all I
see is a call out through port 80 which is usually left open at most sites
for Web surfing.

Have a good one...

Mark Rae said:
[top-posting corrected as usual]
I converted your code example to VB

Why on earth did you do that...?
Error: blank User-Agent header is not allowed! Please ask the author of
your IP address checking client to specify their client's name in the
User-Agent header

Use this URL instead: http://www.netikus.net/show_ip.html
 
Thanks Jamal, that seems to work also.

Jamal said:
Try the following:

Private Sub MyIPaddressButton_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyIPaddressButton.Click
Dim strHostIP As String = String.Empty

Using objWebClient As New WebClient()

Using objStreamReader As StreamReader = New
StreamReader(objWebClient.OpenRead
("http://www.whatismyip.com/automation/n09230945.asp"))

strHostIP = objStreamReader.ReadToEnd()

objStreamReader.Close()

End Using

End Using

End Sub

Jamal
 
Bruce,
My firewall must not be set up properly, as the above solutions worked. I
don't know how port blocking would interfere here as it appears to me we are
just using port 80 which is left open at most sites for web surfing. I am
trying to get the public IP address of the Web Server outside the firewall,
not the internal LAN address.

Please explain, if you have a minute, because now I am concerned about our
firewall here.
Thanks...
 
Back
Top