user3000943
user3000943

Reputation: 13

how to get User IP address in asp.net?

This is the label

 <asp:Label ID="lblIp" runat="server"></asp:Label>

Here Code I used to get user IP address

 string VisitorsIPAddr = string.Empty;

            if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
            {

                VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                lblIp.Text = VisitorsIPAddr;
            }
            else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
            {
                VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
                lblIp.Text = VisitorsIPAddr;
            }

but Always I got the same result. That was 127.0.0.1 and always it goes to else if state.

further

HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] did not give any result. It always give nothing

Upvotes: 1

Views: 1869

Answers (1)

bresleveloper
bresleveloper

Reputation: 6066

You are running the application at your own machine therefor you are the one sliding there, so you will always get 127.0.0.1 in the else if. If you want to see another IP open the app and relevant port and get there with another machine.

Also the HTTP_X_FORWARDED_FOR is not always there (usually from normal web requests it will be but there are exceptions).

I think thats what Kundan Singh Chouhan tried to suggest, hope I am not wrong.

Upvotes: 1

Related Questions