开发者

HttpContext IP Problem

开发者 https://www.devze.com 2022-12-11 11:48 出处:网络
I have a problem when I use HttpContext.Current.Request.UserHostAdd开发者_Go百科ress, some times returns \"192.168.0.17\" (IPv4) and some times returns \"fe80::99be:a05d:7938:1c30%8\" (IPv6), calling

I have a problem when I use HttpContext.Current.Request.UserHostAdd开发者_Go百科ress, some times returns "192.168.0.17" (IPv4) and some times returns "fe80::99be:a05d:7938:1c30%8" (IPv6), calling from the same computer and navigator.

What I do to return always IPv4?


Check out this post on 4GuysFromRolla and see if it helps at all. I think this is the information you're looking for.

https://web.archive.org/web/20201028122055/https://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;
  }


Found a solution that somebody hacked up. Can't say if it'll work, tho =)

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

0

精彩评论

暂无评论...
验证码 换一张
取 消