2009-12-15 279 views
45

我想獲得在我的網站註冊的任何IP地址。如何在ASPNET中做到這一點。我用下面的代碼,但是,它沒有得到正確的IP地址如何獲取IP地址?

string ipaddress = Request.UserHostAddress; 
+0

1 http://www.w3schools.com/asp/coll_servervariables.asp](http://www .w3schools.com/asp/coll_servervariables.asp)2. [http://balanagaraj.wordpress.com/2008/01/07/get-users-country-name-using-ip-address/](http:// balanagaraj.wordpress.com/2008/01/07/get-users-country-name-using-ip-address/) – solairaja 2009-12-15 12:50:02

+1

[如何在ASP.NET中獲取用戶的客戶IP地址?](http:/ /stackoverflow.com/questions/735350/how-to-get-a-users-client-ip-address-in-asp-net) – 2015-06-10 17:54:28

回答

54

您可以使用此方法來獲取客戶機的IP地址。

public static String GetIP() 
{ 
    String ip = 
     HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 

    if (string.IsNullOrEmpty(ip)) 
    { 
     ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
    } 

    return ip; 
} 
+1

拆分HTTP_X_FORWARDED_FOR並選擇最後一個更好 - 查看答案http://stackoverflow.com/a/13249280/52277 – 2014-05-01 05:08:10

+2

'X-Forwarded-For'可以包含多個IP地址。 – Gabriel 2014-11-03 00:50:39

3

如果一個客戶端連接,通過透明的非匿名代理,你可以從他們的地址:

Request.ServerVariables["HTTP_X_FORWARDED_FOR"] 

可以返回null或「未知」如果無法獲得IP那樣。

Request.ServerVariables["REMOTE_ADDR"]應該與Request.UserHostAddress相同,如果請求不是來自非匿名代理,則可以使用其中的任一個。

但是,如果請求來自匿名代理,則無法直接獲取客戶端的IP。這就是爲什麼他們稱這些代理匿名

34

應該使用HTTP_X_FORWARDED_FOR,但它可以返回多個由逗號分隔的IP地址。見this page

所以你應該經常檢查它。我個人使用分割功能。

public static String GetIPAddress() 
{ 
    String ip = 
     HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 

    if (string.IsNullOrEmpty(ip)) 
     ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
    else 
     ip = ip.Split(',')[0]; 

    return ip; 
} 
+17

我會小心的,你選擇的第一個項目可能是192.168.x.x.您通常希望獲得最後一個,因爲那是連接到您的系統的最後一個代理或客戶端(可能是最可靠的來源)。見[維基百科文章](http://en.wikipedia.org/wiki/X-Forwarded-For)我使用的代碼是這樣的:'ip =(HttpContext.Request.ServerVariables [「HTTP_X_FORWARDED_FOR」] ??「」 ).Split(',')。Last()。Trim();' – 2012-11-06 10:30:19

+4

@WouterSimons,根據[Wikipedia article](https://en.wikipedia.org/wiki/X-Forwarded-For)「最左邊的是原始客戶端「,所以應該使用First()而不是Last() – 2015-11-25 06:36:01

33

在您使用IP地址進行安全保護的情況下,您應該瞭解您的基礎設施。

如果您在Web服務器和設置標頭的客戶端之間使用代理,則應該可以信任最後一個地址。然後你使用像穆罕默德建議的更新代碼,總是從轉發頭獲得最後一個IP地址(見下面的代碼)

如果你不使用代理,請注意X-Forwarded-For頭是非常容易欺騙。我建議你忽略它,除非你有明確的理由不這樣做。

我更新穆罕默德·阿赫塔爾的代碼如下,讓你選擇:

public string GetIP(bool CheckForward = false) 
{ 
    string ip = null; 
    if (CheckForward) { 
     ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 
    } 

    if (string.IsNullOrEmpty(ip)) { 
     ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
    } else { // Using X-Forwarded-For last address 
     ip = ip.Split(',') 
       .Last() 
       .Trim(); 
    } 

    return ip; 
} 

Wikipedia article更全面地說明了風險。

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

請向您的代碼添加解釋(並改進縮進)。這會提高你的答案的質量! – 2016-12-02 12:20:23

+0

你永遠不會使用'le'變量 - 我想你想在下一行使用那個而不是'0' – 2016-12-02 12:36:29

4

在MVC 6,您檢索IP地址這種方式:

HttpContext.Request.HttpContext.Connection.RemoteIpAddress.ToString()