Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For my rating functionality i need to get the User's System(PC) IP address and not the N/W provider IP in c#.
I tried following but not worked:

string host = Dns.GetHostName();
        //IPHostEntry local = Dns.GetHostByName(host);
        //IPAddress ipadd = local.AddressList[0];


C#
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (ipAddress == null || ipAddress == "")
        {
            ipAddress = Request.ServerVariables["REMOTE_ADDR"];
        }


please help me ASAP

Thanks
Posted
Updated 12-Oct-11 2:40am
v2

If you want your external IP address, use an external service:

http://www.whatismyip.com/[^]

Send a get request to the URL above and strip out the IP address, or even get the IP address from the Title of the page:

(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)


OK for those who want it:

using System.Net;
using System.Text;

namespace DreamInCode.Snippets
{
    public static class IPFinder
    {
        private static readonly UTF8Encoding utf8 = new UTF8Encoding();

        public static IPAddress ExternalIPAddress
        {
            get
            {
                string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp";
                WebClient wc = new WebClient();
                string response = utf8.GetString(wc.DownloadData(whatIsMyIp));
                IPAddress myIPAddress = IPAddress.Parse(response);

                return myIPAddress;
            }
        }
    }
}


My source: http://www.dreamincode.net/code/snippet959.htm[^]

NOTE: The source page has the OLD automation address - use the snippet above which I have modified to use the new one.
 
Share this answer
 
v2
Comments
krishnaMurali 12-Oct-11 8:51am    
can u provide code plz
C#
string IP = GetIP();

    public string  GetIP()
    {
        string Str = "";
        Str = System.Net.Dns.GetHostName();
        IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(Str);
        IPAddress[] addr = ipEntry.AddressList;
        return addr[addr.Length - 1].ToString();

    }
 
Share this answer
 
 
Share this answer
 
 
Share this answer
 
Try this --

C#
using System.Net;

string strHostName = string.Empty;
strHostName = Dns.GetHostName();

IPHostEntry ipEntry = Dns.GetHostByName(strHostName);

IPAddress [] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
Response.Write("IP Address {0}: {1} " + i + addr[i].ToString() + ", HostName: " + strHostName);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900