domster
domster

Reputation: 566

Bypass Proxy Server, ASP Website on IIS

I've actually deployed my asp website on IIS and everything works well. I have done some reading on how to bypass the proxy server but i'm still unsure on how to do it. The reason why i want to bypass the proxy server on the network is to be able to read client's ip address (the ip address behind the proxy). Frankly speaking, i don't have much knowledge on this topic..

I read from MSDN that you can add in lines of codes into Web.Config file to bypass the proxy server. But i'm not sure what to type in or how to use this defaultproxy tag.

Link

Right now i'm only retrieving either 127.0.0.1 which is localhost or the machine's network external ip address which is not what i want..

Anyone can point me in the right direction to getting ip address behind the proxy? Appreciate any help please. Thank you..

Codes i have been experimenting to get client's IP address:

protected void getIP_Click(object sender, EventArgs e)
{
    String hostName = System.Net.Dns.GetHostName();
    String clientIpAddressHostName = System.Net.Dns.GetHostAddresses(hostName).GetValue(0).ToString();
    IP1.Text = clientIpAddressHostName;

    String clientIpAddress = HttpContext.Current.Request.ServerVariables["REMOTE_HOST"].ToString();
    IP2.Text = clientIpAddress;

    String ip = null;

    if (String.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
    {
        ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }

    IP3.Text = ip;

    String ipNext = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
    IP4.Text = ipNext;

    //String ipNextNext = HttpContext.Current.Request.UserHostAddress;
    String ipNextNext = HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"].ToString();
    IP5.Text = ipNextNext;

    String last = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();
    IP6.Text = last;

    Label2.Text = getIPAdd();

    try
    {
        String externalIP;
        externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
        externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")).Matches(externalIP)[0].ToString();
        Label1.Text = externalIP;
    }
    catch (Exception ex)
    {
        logManager log = new logManager();
        log.addLog("IP Add", "IP Add", ex);
    }
}

private String getIPAdd()
{
    System.Web.HttpContext context = System.Web.HttpContext.Current;
    string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (!string.IsNullOrEmpty(ipAddress))
    {
        string[] addresses = ipAddress.Split(',');
        if (addresses.Length != 0)
        {
            return addresses[0];
        }
    }

    return context.Request.ServerVariables["REMOTE_ADDR"];
}

Results,

Results

Requested Details:

details

Upvotes: 0

Views: 1583

Answers (1)

vorou
vorou

Reputation: 1909

I just used this to get the client IP address:

HttpContext.Current.Request.UserHostAddress

As I can see you have that one in your code too, but it's commented out. It worked for me:

enter image description here

Upvotes: 1

Related Questions