Saturday 30 August 2014

How to Get User IP address in ASP.NET using C#/VB.NET


Get Ip Address of user
Description:-

In this example we explain that how to get IP Address of the client machine in asp.net with C#.or fetch/find IP address of the client or server machine using asp.net with C#.

You can also put this code in your website to fetch or retrieve users IP address and also you count the unique visitor in your website.

We all show that in many website use this type of functionality so he can view that which user is visited our website. So you can also use or set this code to your website for getting the list of IP address of the users machine that are visited your website.

How to Bind Excelsheet Data to Gridview CRUD operation in Excelsheet Database

How to Display Layer on one Another in CSS Overlapping Layer in CSS
Default3.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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 id="Head1" runat="server">
    <title>How to Get Ip Address of user's system in asp.net using C#
    </title>
  
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

Default3.aspx.cs:- 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{



    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = getIpAddressAddress();

    }
    public static string getIpAddressAddress()
    {
        string IpAddress = string.Empty;
        IpAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (!string.IsNullOrEmpty(IpAddress))
        {
            string[] IpAddressRange = IpAddress.Split(',');
            int le = IpAddressRange.Length - 1;
            string trueIpAddress = IpAddressRange[le];
        }
        else
        {
            // IpAddress = Request.ServerVariables("REMOTE_ADDR");
            IpAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }

        return "Your system IpAddress is: " + IpAddress;
    }



}


0 comments:

Post a Comment