C# - Get User IP Address

By , 6/22/2012
(1 ratings)
Below code snippet can be used to get users IP address in asp.net
You can use below function like
string ipaddress = GetSystemIPAddress();
HttpContext.Current.Response.Write(ipaddress);
Translate to VB
 public static string GetSystemIPAddress()
        {

            System.Web.HttpContext context = System.Web.HttpContext.Current;

            string strIPAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (string.IsNullOrEmpty(strIPAddress))
            {

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

            }

            else
            {

                string[] stripArray = strIPAddress.Split(new Char[] { ',' });

                return stripArray[0];

            }

        }
Tagged with IP ADDRESS ASP.NET.

Comments

 

Log in, to comment!