Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the IP address using C# in asp.net

Tags:

c#

asp.net

ip

I want to get the public IP address of the visitor in my code.

I have written below code to get it:

        var context = System.Web.HttpContext.Current;
        string ip = String.Empty;

        if (context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
        {
            ip = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
        }
        else if (!String.IsNullOrWhiteSpace(context.Request.UserHostAddress))
        {
            ip = context.Request.UserHostAddress;                
        }

        if (ip == "::1")
            ip = "127.0.0.1";

        return ip;

I am not getting the exact IP address It returns the value like: fe80::9419:dfb3:22ce:4e88%68 but when I see my IP in What is my IP? it shows 13.67.58.30. How would I get the exact IP address?

like image 273
Mohemmad K Avatar asked Feb 02 '16 09:02

Mohemmad K


2 Answers

I would recommend against using Request.ServerVariables["HTTP_X_FORWARDED_FOR"]. Reason being that it's being passed by the X-Forwarded-For HTTP header, and all HTTP headers can be spoofed. If using your code, a user could impersonate any IP they wanted by simply replacing the header. Also, it's not guaranteed that all proxies will even place that header in the first place.

You can get the user's IP address by using:

string ip = context.Request.UserHostAddress;

That number you're seeing is most likely the client's IPv6 address.

like image 175
Gediminas Masaitis Avatar answered Sep 22 '22 18:09

Gediminas Masaitis


UserHostAddress. This method gets the IP address of the current request. It uses the UserHostAddress property in the ASP.NET framework. This is the easiest way to get a string representation of the IP address. Example. First, this sample code presents the Application_BeginRequest method, which is executed every time a user visits the web site. You can add it by going to Add -> Global Application Class. In Application_BeginRequest, we get the current HttpRequest, then access the UserHostAddress string property. Finally, we write the string value to the output and complete the request. Method that gets IP address: ASP.NET, C#

using System;
using System.Web;

namespace WebApplication1
{
    public class Global : HttpApplication
    {
    protected void Application_BeginRequest(object sender,
        EventArgs e)
    {
        // Get request.
        HttpRequest request = base.Request;

        // Get UserHostAddress property.
        string address = request.UserHostAddress;

        // Write to response.
        base.Response.Write(address);

        // Done.
        base.CompleteRequest();
    }
    }
}

Result of the application

127.0.0.1

The IP address. In this example, I ran the program on the localhost server, which is one on my computer. In this case, the connection is only a local connection, which means my local address was the IP address returned. Note: If this code was run on a remote server, the IP address for my internet connection would be returned.

Summary. We acquired the current IP address of the user with the UserHostAddress property. This method returns a string with numbers separated by periods. This can be directly used in a Dictionary if you need to record or special-case users by IP.

like image 22
mohammad hosein Ezati Avatar answered Sep 19 '22 18:09

mohammad hosein Ezati