2011-10-06 11 views
9
  1. In Dev Machine HttpContext.Current.Request.UserHostAddress è null. Perché? come posso accenderlo?
  2. Come posso ottenere l'elenco degli IP in caso di un client proxy?

Servizio WCF con ASP.net 4 window7.HttpContext.Current.Request.UserHostAddress è null

Grazie

+0

ho mai ottenere un vuoto/null, ma Cassini restituisce un ":: 1" ora per qualsiasi motivo. –

+0

@DoozerBlake the :: 1 è la versione di IPv6 di 127.0.0.1. Fonte: http://stackoverflow.com/a/4611421/84395 –

risposta

6

per evitare questo problema è possibile analizzare il HTTP_X_FORWARDED_FOR per l'ultimo IP entery.

ip=Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ; 
if (!string.IsNullOrEmpty(ip)) 
{ 
    string[] ipRange = ip.Split(','); 
    int le = ipRange.Length - 1; 
    string trueIP = ipRange[le]; 
} 
else 
{ 
    ip=Request.ServerVariables["REMOTE_ADDR"]; 
} 

auguriamo che questo ti aiuta

+3

Solo una nota sulla risposta di 'Viral Sarvaiya'. Secondo [Wikipedia] (http://en.wikipedia.org/wiki/X-Forwarded-For) e [Amazon] (http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/TerminologyandKeyConcepts.html) l'intestazione X-Forwarded-For ha il seguente formato:> X-Forwarded-For: client, proxy1, proxy2 Ciò significa che dovresti usare la prima stringa in 'ipRange', non l'ultima. – dubrowgn