¿Cómo obtener la IP del usuario actual con ASP.NET?2043

¿Cómo obtener la IP del usuario actual con ASP.NET?

En este artículo voy a explicar cómo obtener la dirección IP de los visitantes a tu sitio web en ASP.Net. Básicamente, existen dos formas para buscar una IP:

  1. Para encontrar la dirección IP de una máquina detrás de LAN
  2. Para encontrar la dirección IP detrás de proxy o máquina cliente

Enseguida puedes ver el ejemplo:

 

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="ObtenerIP_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Obtener dirección IP y Host Name</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-weight:bold; border:1px;">
        Tu dirección IP es: <asp:Label ID="lblIPAddress" runat="server" Text=""></asp:Label><br /><br />
        Tu computadora/Host Name es: <asp:Label ID="lblHostName" runat="server" Text=""></asp:Label>
        <br /><br />
        Tu dirección IP detrás del Proxy es: <asp:Label ID="lblIPBehindProxy" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>
Imports System.Net

Partial Class ObtenerIP_Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim strHostName As String = Dns.GetHostName()
        Dim ipEntry As IPHostEntry = Dns.GetHostEntry(strHostName)

        lblIPAddress.Text = Convert.ToString(ipEntry.AddressList(ipEntry.AddressList.Length - 1))
        lblHostName.Text = Convert.ToString(ipEntry.HostName)

        'Find IP Address Behind Proxy Or Client Machine In ASP.NET
        Dim IPAdd As String = String.Empty
        IPAdd = Request.ServerVariables("HTTP_X_FORWARDED_FOR")

        If String.IsNullOrEmpty(IPAdd) Then
            IPAdd = Request.ServerVariables("REMOTE_ADDR")
            lblIPBehindProxy.Text = IPAdd
        End If
    End Sub
End Class

Obtener IP con asp.net

Si quieres descargar el ejemplo, lo puedes hacer en la siguiente liga: https://github.com/jiestrada/ObtenerIP

Espero que sea de utilidad este ejemplo, si tienes alguna pregunta no dudes en escribirme.

Compartir

Comentarios (20)

  • Juan Carlos Responder

    Esto te da la IP, HOSTNAME del servidor donde está apuntando la página, más NO te devuelve los datos del cliente que accede.

    Monday, October 14, 2013 10:40 AM
    • José I. Estrada Hdz Responder

      Hola Juan Carlos, Gracias por tus comentarios, revise lo que me comentas y en mi caso si puedo obtener la IP con la que visito el sitio web, puedes revisar esta liga de ejemplo: http://www.developerji.com/obtenerIP.aspx Ahí podras ver el apartado: Tu dirección IP detrás del Proxy es: 187.174.108.21 La obtengo con esta parte del código: If String.IsNullOrEmpty(IPAdd) Then IPAdd = Request.ServerVariables("REMOTE_ADDR") lblIPBehindProxy.Text = IPAdd End If En mi caso esa es mi IP, checalo y lo puedes corroborar con cualquier página que te proporcione tu IP en internet. http://www.obtenerip.com.ar/ip/ http://cual-es-mi-ip-publica.com/ http://www.vermiip.es/ http://www.cualesmiip.com/ Saludos y gracias por tus comentarios, si algo no funciona estare agradecido que me lo hagas saber para revisarlo.

      10/14/2013 2:24:51 PM
  • Manuel Responder

    Hola que tal amigo he probado tu codigo pero no me deuvelve una direccion ip me devuelve esto mira fe80::f06f:935f:21a8:f496%10 de antemano muchas gracias. saludos

    Thursday, September 25, 2014 11:55 AM
  • Christian Responder

    Excelente aporte amigo mio... me funciono de maravilla... a pesar de llevar demasiado poco tiempo en asp.net con tu aporte logre parte de lo que queria... pero sabes... kizas tu me puedas ayudar a concretar definiticamente con lo que deseo hacer... la idea es guardar esta info en una base de datos sql, excel o block de notas de cada visitante. Me podrias orientar con tus conocimeintos amigo mi, como poder hacerlo o facilitarme algun codigo de ejemplo. Muchas gracias Maestro!!!!

    Friday, November 13, 2015 1:17 PM
  • Christian Responder

    Estimado José I. Estrada Hdz, me sucede lo mismo que a Manuel, de forma local ningun problema el codigo, pera al ser utilizado para saber las ips de quienes visitan las pagina, aparece lo siguiente Tu dirección IP es: 2001:0:9d38:6ab8:482:19d5:2fa2:993 Tu computadora/Host Name es: vj4980. Saludos!!

    Friday, November 13, 2015 6:06 PM
  • Raynor Responder

    What's up colleagues, how is everything, and what you wish for to say concerning this post, in my view its truly awesome for me.

    Sunday, April 23, 2017 7:42 AM
  • Loftis Responder

    Hi mates, how is the whole thing, and what you want to say on the topic of this post, in my view its genuinely remarkable in support of me.

    Wednesday, April 26, 2017 7:58 AM
  • Wyrick Responder

    Oh my goodness! Incredible article dude! Thank you, However I am experiencing difficulties with your RSS. I don't know why I am unable to subscribe to it. Is there anybody having the same RSS issues? Anyone that knows the answer can you kindly respond? Thanks!!

    Sunday, July 16, 2017 7:30 AM
  • Sansom Responder

    Good day! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I'm getting fed up of Wordpress because I've had problems with hackers and I'm looking at alternatives for another platform. I would be fantastic if you could point me in the direction of a good platform.

    Sunday, July 16, 2017 3:53 PM
  • Sons Responder

    Good day very cool blog!! Guy .. Beautiful .. Wonderful .. I will bookmark your web site and take the feeds also? I am glad to seek out so many helpful information here within the publish, we need work out extra techniques on this regard, thank you for sharing. . . . . .

    Monday, July 17, 2017 2:10 PM
  • Vanatta Responder

    Hi there! This is my first visit to your blog! We are a collection of volunteers and starting a new project in a community in the same niche. Your blog provided us useful information to work on. You have done a outstanding job!

    Tuesday, July 18, 2017 4:03 AM
  • Dew Responder

    It's great that you are getting ideas from this piece of writing as well as from our discussion made at this place.

    Wednesday, August 16, 2017 11:55 AM
  • Erwin Responder

    Hello there, just became alert to your blog through Google, and found that it's truly informative. I'm gonna watch out for brussels. I will apreciate if you continue this in future. A lot of people will be benefited from your writing. Cheers!

    Tuesday, August 29, 2017 1:46 AM
  • Burley Responder

    What's up everyone, it's my first visit at this website, and article is really fruitful designed for me, keep up posting these types of posts.

    Thursday, September 21, 2017 2:26 PM
  • Pendergrass Responder

    Have you ever considered about including a little bit more than just your articles? I mean, what you say is valuable and all. However just imagine if you added some great graphics or videos to give your posts more, "pop"! Your content is excellent but with images and videos, this website could undeniably be one of the most beneficial in its niche. Very good blog!

    Sunday, November 12, 2017 7:16 PM
  • Scherf Responder

    Hi there, I log on to your blog like every week. Your story-telling style is witty, keep doing what you're doing!

    Tuesday, November 14, 2017 12:44 PM
  • Martinovich Responder

    These are really wonderful ideas in regarding blogging. You have touched some fastidious points here. Any way keep up wrinting.

    Wednesday, November 15, 2017 1:44 PM
  • Landon Responder

    I have noticed you don't monetize your site, don't waste your traffic, you can earn additional bucks every month because you've got hi quality content. If you want to know how to make extra $$$, search for: Boorfe's tips best adsense alternative

    Thursday, November 16, 2017 6:50 PM
  • Donaldson Responder

    Hi friends, its enormous paragraph about educationand completely defined, keep it up all the time.

    Thursday, November 16, 2017 9:50 PM
  • Glade Responder

    Hi! I just would like to give you a big thumbs up for the excellent info you have got here on this post. I am returning to your site for more soon.

    Friday, November 17, 2017 4:10 PM

Animate deja un comentario!!!

Eres un humano?

Últimos Tweets

Contáctanos

  • Dirección: Plutarco Elias Calles 243, Granjas México, Iztacalco, DF.

  • Teléfono:55-2712-3820 o 55-4164-9484

  • Email: info@developerji.com