HOW TO FIND CLIENT IP ADDRESS OF USER IN ASP.NET C#


Today in Engineer's World, we are discussing ASP.NET & C# query faced by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS 

Click imaginationhunt.blogspot to see latest Blogs


HOW TO FIND CLIENT IP ADDRESS OF USER IN ASP.NET C#

In our previous blog, we have prepared the design. Now, it’s time to create the logic to fetch the IP Address and bind them with label controls.
Now, you are thinking why we have taken four labels? Actually, we have 2 different ways to get the IP Address, that’s why we have taken 2 labels.

Things to Remember: (Before start writing code)

1. “System.Net” Namespace – This namespace provides a simple programming interface for various protocols that used on the network. Read more about namespaces
2. “System.Web” Namespace – This namespace contains types that enable browser/server communication. Read more about namespaces
For e.g. - “HttpContext” is a type that is used to encapsulate all HTTP-specific information about an individual HTTP request.

Practical Implementation:

View Code Behind

using System;
using System.Web;
using System.Net;

public partial class GetIPAddress : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "Label1: " + GetIP1();
        Label2.Text = "Label2: " + GetIP2();
    }

    //Get IPAddress Method 1
    public string GetIP1()
    {
        string ipaddress;
        try
        {
            ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (ipaddress == "" || ipaddress == null)
            {
                ipaddress = Request.ServerVariables["REMOTE_ADDR"];
                return ipaddress;
            }
            return ipaddress;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    //Get IPAddress Method 2
    protected string GetIP2()
    {
        try
        {
            string VisitorsIPAddr = string.Empty;
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
            {
                VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
            {
                VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
            }
            return VisitorsIPAddr;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
}

Debug and Run the application. On the output screen, you may find the fetched IP Address in the respected labels.

Output:



Hence, you can find IP Address by any of these methods. Good Luck!!!


Related Questions:


Q-1 What is DNS?
Ans- DNS – DNS stand for Domain Name System/Server/Service. It is a system which translates or convert the domain name into an IP Address.
For e.g. – DNS translate www.liveviar.com to 182.50.151.47

Q-2 What is the localhost assigned IP for IPV4 and IPV6?
Ans- Localhost means services (i.e. application) and the server is hosted on the same machine and its hosted services can’t be accessed outside.
For IPV4 it is 127.0.0.1
For IPV6 it is 0.0.0.0.0.0.0.1 or ::1

IP Address 127.0.0.1 for IPV4 and 0.0.0.0.0.0.0.1/::1 for IPV6 are most commonly used loopback address.

Q-3 Which namespaces are used for the networking operation in Asp.net C#?
A) System.Net
B) System.Net.Mail
C) System.Web
D) System.Web.Mail
E) All the above mentioned
Ans- Option (A) and (B).

Q-4 How many ports of TCP/IP are reserved for specific protocols?
A) 10
B) 1024
C) 2048
D) 512
Ans- Option (B)

Q-5 How many bits are present in a single IP address?
A) 8
B) 16
C) 32
D) 64
Ans- Option (C)

Click imaginationhunt.blogspot to see latest blogs

Keep learning and sharing…

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions