2012-09-26 15 views

cevap

16
IP = ((HttpContextBase)request.Properties["MS_HttpContext"]).Request.UserHostAddress; 
6

this göre, daha kapsamlı bir şekilde olacaktır: MVC 3 projelerde (değil API) üzerinde Geçmişte

private string GetClientIp(HttpRequestMessage request) 
{ 
    if (request.Properties.ContainsKey("MS_HttpContext")) 
    { 
     return ((HttpContext)request.Properties["MS_HttpContext"]).Request.UserHostAddress; 
    } 
    else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name)) 
    { 
     RemoteEndpointMessageProperty prop; 
     prop = (RemoteEndpointMessageProperty)this.Request.Properties[RemoteEndpointMessageProperty.Name]; 
     return prop.Address; 
    } 
    else 
    { 
     return null; 
    } 
} 

, aşağıdaki kullanmak için kullanılır:

string IPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 

if (String.IsNullOrEmpty(IPAddress)) 
    IPAddress = Request.ServerVariables["REMOTE_ADDR"]; 
+1

Biraz fazladan araştırma yaptım çünkü tutacak kadar garip geldi Bir sunucu değişkeninde bir istek başlığı. context.Request.ServerVariables ["HTTP_X_FORWARDED_FOR"], Proxy Servers ve Load Balancers tarafından gönderilen X-Forward-For isteği üstbilgisini toplayanı seçiyor. – muglio

11

Aşağıdaki kodu kullanıyorum ve benim için çalışıyorum ....

string ipAddress = System.Web.HttpContext.Current.Request.UserHostAddress; 
+0

Ev sahibi adresi verir –

İlgili konular