Link to home
Start Free TrialLog in
Avatar of Jugnoo2013
Jugnoo2013

asked on

IP address of client from MVC ASP.net application

What is the best foolproof way of getting client IP address for the client who is behind firewall, proxy or load balancer in ASP.net to harden the security of the site?
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

If this is on the public internet, the only thing you can get is their public IP address.  It's reported in the server variables under REMOTE_ADDR.

This simple program reports all the server variables available on your server.  REMOTE_ADDR is added separately at the bottom.
<%@ Page Language = "VBScript" AspCompat="True" %>
<% Response.Buffer = true %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>ASPX .NET Server Variables</title>
</head>
<body>
<h1 align="center">ASPX .NET Server Variables</h1>
<%
Dim loop1, loop2 As Integer
Dim arr1(), arr2() As String
Dim coll As NameValueCollection

' Load ServerVariable collection into NameValueCollection object.
coll=Request.ServerVariables 
' Get names of all keys into a string array.
arr1 = coll.AllKeys 
For loop1 = 0 To arr1.GetUpperBound(0)
   Response.Write("<b>" & arr1(loop1) & "</b>: ")
   arr2 = coll.GetValues(loop1) ' Get all values under this key.
   For loop2 = 0 To arr2.GetUpperBound(0)
      Response.Write("Value " & CStr(loop2) & ": " & Server.HtmlEncode(arr2(loop2)) & "<br>")
   Next loop2
Next loop1

Response.Write ("<br>" & vbCrLf)
Response.Write ("REMOTE_ADDR" & ": " _
	& Request.ServerVariables("REMOTE_ADDR") _
	& "<br>" & vbCrLf)

%>

</body>
</html>

Open in new window

Avatar of Jugnoo2013
Jugnoo2013

ASKER

Thanks, I will take a look. Does same hold true for intranet sites?
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial