Speed up development with full-stack environments for every branch.

Learn More

Retrieving User's IP Address using ASP.NET

6377 Runs 52487 Views 26661 Copies
Saved

Saved

mirang 126

mirang
published 4 years ago

<%@ Page Language="C#" %>

<!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>
   <title>User's IP Address using ASP.NET</title>
    <script language="C#" runat="server">
        public void Page_Load(Object sender, EventArgs e)
        {
            //Print the time when the page loaded initially
            Response.Write("<span class='logo' />The Page Loaded at: " + DateTime.Now.ToLongTimeString());
            divLANIPAddress.InnerHtml = GetLanIPAddress().Replace("::ffff:","");
        }
        /*
         Method to get the IP Address of the User
         */
        public String GetLanIPAddress()
        {
            //The X-Forwarded-For (XFF) HTTP header field is a de facto standard for identifying the originating IP address of a 
            //client connecting to a web server through an HTTP proxy or load balancer
            String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        
            if (string.IsNullOrEmpty(ip))
            {
                ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
        
            return ip;
        }
    </script>
    <style type="text/css">
    body
    {
      background-color:#32373a;
      color:#FFFFFF;
    }
    #mainBody
    {
      background-color:#FFFFFF;
      height:100%;
      color:#32373a;
    }
    .divMainTime
    {
      width:350px;
      height:30px;
      background-color:#fdd136;
      font-size:14px;
      vertical-align:middle;
      padding-top: 5px;
    }
    #divLANIPAddress
    {
        font-size:20px;
        float:right;
        margin-right:10px;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    
    <div id='mainBody'>
    <h1>
    Retrieve User's IP Address
    </h1>
    <br />
        <div class='divMainTime'>
            <div style='float:left;font-size:18px;'>User's IP Address :</div> 
            <div id="divLANIPAddress" runat="server"></div>
        </div>
    </div>
    </form>
</body>
</html>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).