Come posso ottenere l'indirizzo IP dei visitatori correnti?Ottieni l'indirizzo IP dell'utente
risposta
HttpContext.Current.Request.UserHostAddress;
o
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
o
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
Request.UserHostAddress
Modifica: ha anche trovato una domanda interessante riguardante le intestazioni http relative all'IP here.
Edit2: Come menzionato nei commenti e nel collegamento che ho fornito sopra, l'intestazione HTTP_X_FORWARDED_FOR
può contenere più indirizzi IP separati da virgola. Non ho affrontato questa situazione ma suppongo che siano necessarie alcune correzioni alla mia risposta.
Io uso questo codice per ottenere l'indirizzo IP (restituisce IPAddress.None
valore se sempre fallito per qualche motivo):
/// <summary>
/// Gets the IP address of the request.
/// <remarks>
/// This method is more useful than built in because in some cases it may show real user IP address even under proxy.
/// <summary>
/// Gets the IP address of the request.
/// <remarks>
/// This method is more useful than built in because in some cases it may show real user IP address even under proxy.
/// The <see cref="System.Net.IPAddress.None" /> value will be returned if getting is failed.
/// </remarks>
/// </summary>
/// <param name="request">The HTTP request object.</param>
/// <returns></returns>
public static IPAddress GetIp(this HttpRequest request)
{
string ipString;
if (string.IsNullOrEmpty(request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
{
ipString = request.ServerVariables["REMOTE_ADDR"];
}
else
{
ipString = request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault();
}
IPAddress result;
if (!IPAddress.TryParse(ipString, out result))
{
result = IPAddress.None;
}
return result;
}
+1 per il ritorno come oggetto Indirizzo IP –
Si noti che l'intestazione 'X-Forwarded-For' può essere falsificata, quindi non prendere decisioni sulla sicurezza basate su di essa. Controllare solo il valore 'HTTP_X_FORWARDED_FOR' se la macchina si trova dietro un servizio di bilanciamento del carico o un proxy inverso. – devstuff
Ricorda inoltre che HTTP_X_FORWARDED_FOR conterrà più indirizzi IP se vengono inoltrati più proxy. Il metodo Parse lo gestisce correttamente? – Tomas
public String GetIP()
{
string ipString;
if (string.IsNullOrEmpty(Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
{
ipString = Request.ServerVariables["REMOTE_ADDR"];
}
else
{
ipString = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
}
return ipString;
}
Prima cercando di scoprire IP proxy, se nulla si può ottenere che IP del sistema
Prova questo per ottenere l'indirizzo IP esterno di utenza ..
public static string getExternalIp()
{
try
{
string externalIP;
externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
.Matches(externalIP)[0].ToString();
return externalIP;
}
catch { return null; }
}
Qual è il diverso tra le prime due alternative? –