connectseekers.com

Friday, 13 June 2014

How to get a User's Client IP address in ASP.NET using C#.Net

6/13/2014 - By Pranav Singh 0

In this article I will show you how you how to get a user's client IP address in ASP.NET using C#.Net. 


Now here is the code for getting 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)
        {
            //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()
        {
            //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;
        }
       }
}

Now we have some please check the output.



The above address is my localhost IP




About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top