In this post I am going to tell you how to get IP Address of the client machine in ASP.NET 5 with C#.Net or fetch/discover IP location of the client or server machine utilizing ASP.NET with C#.Net. You can likewise put this code in your site to get or retrieve users IP address also you count the unique visitor in your website. In numerous site use this kind of usefulness so he can see that which user is visited our website. So you can likewise utilize or set this code to your site for getting the listof IP location of the users machine that are gone by your site.


The following code is to get the client IP address in ASP.NET:
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="RetriveUserIpAddressInAspNet.aspx.cs" Inherits="ProjectDemo_Asp.et.RetriveUserIpAddressInAspNet" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Client Id Address:
        <asp:Label ID="lblIPddress" runat="server" Text=""
            style="color: #FF0066; font-size: x-large"></asp:Label>  
    </div>
    </form>
</body>
</html>


C#.Net code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectDemo_Asp.et
{
       public partial class RetriveUserIpAddressInAspNet : System.Web.UI.Page
       {
        public void Page_Load(Object sender, EventArgs e)
        {
            //code for print the time when the page loaded initially        
            lblIPddress.Text= GetLanIPAddress().Replace("::ffff:", "");
        }
        /*
         Method to get the IP Address of the User
         */
        public String GetLanIPAddress()
        {
            String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(ip))
            {
                ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            return ip;
        }
       }
}

HostForLIFE.eu ASP.NET 5 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.