2009-11-12 71 views
4

當我使用HttpContext.Current.Request.UserHostAddress時,有時會返回「192.168.0.17」(IPv4),有時會返回「fe80 :: 99be:a05d:7938:1c30%8」(IPv6)同一臺電腦和導航儀。HttpContext IP問題

我該做什麼才能返回IPv4?

回答

2

找到了一個解決方案,有人被黑了。不能說,如果它會工作,壽=)

http://www.eggheadcafe.com/software/aspnet/30078410/request-object.aspx

+0

感謝您的解決方案。 +1 – Cesar 2009-11-13 10:01:25

+0

僅用幾秒鐘就可以完全擊敗我。 +1 – 2009-11-13 15:26:03

+0

@ md5sum:那麼好消息是,我們沒有人值得實際代碼的信譽;) – 2009-11-13 15:31:51

3

檢查出4GuysFromRolla這篇文章,看看是否有幫助的。我認爲這是你正在尋找的信息。

http://aspnet.4guysfromrolla.com/articles/071807-1.aspx

〜〜的md5sum

public static string GetIP4Address() 
    { 
    string IP4Address = String.Empty; 

    foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress)) 
    { 
     if (IPA.AddressFamily.ToString() == "InterNetwork") 
     { 
     IP4Address = IPA.ToString(); 
     break; 
     } 
    } 

    if (IP4Address != String.Empty) 
    { 
     return IP4Address; 
    } 

    foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName())) 
    { 
     if (IPA.AddressFamily.ToString() == "InterNetwork") 
     { 
     IP4Address = IPA.ToString(); 
     break; 
     } 
    } 

    return IP4Address; 
    } 
+0

http://stackoverflow.com/a/8597351/2788478 – 2017-03-20 17:06:15