Friday 28 March 2014

get IP address from client machine





public string GetIPAddress()
    {
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        string MyIP = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (!string.IsNullOrEmpty(MyIP))
        {
            string[] addresses = MyIP.Split(',');
            if (addresses.Length != 0)
            {
                return addresses[0];
            }
        }
        return context.Request.ServerVariables["REMOTE_ADDR"];
    }


//And You can calll this method as following where you want get the IP Client Address

string ip = GetMyIPAddress();

No comments:

Post a Comment



Asp.net tutorials