Get ip address in asp.net using c#

In this programming tutorial we will learn how to get ip address in asp.net with c#. I have already written same tutorial for php. In asp.net it is also very easy to find ip address no matter whether user is behind the proxy or not, you can easily track the user in asp.net. So let's have a look over the following code snippet.
How to get ip address in asp.net using c#
//To get the ip address of the machine and not the proxy use the following code     
            string strIPAddress = Request.UserHostAddress.ToString();
            strIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (strIPAddress == null || strIPAddress == "")
            {
                strIPAddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
            }

So that's it.
I love your feedback.

0 comments: