Method of obtaining IP address by. Net

Time:2020-3-15

Get native IP

1.png

2. Get the IP address of the web client

2.png

/// <summary>    
  ///Get IPv4 address    
  /// </summary>    
  /// <returns>
  </returns>  
  public static string GetIP4Address(HttpRequest request = null)    
  {      
  Func<IPAddress, bool> f = e => e.AddressFamily.ToString() == "InterNetwork";      
  IPAddress ipa = Dns.GetHostAddresses((request
  ?? HttpContext.Current.Request).UserHostAddress).FirstOrDefault(f)        
  ?? Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault(f);      
  if (ipa != null)     
  {       
  return ipa.ToString();    
  }      
  return HttpContext.Current.Request.UserHostAddress;   
  }

summary

The above is the method of. Net getting IP address introduced by Xiaobian to you. I hope it can help you. If you have any questions, please leave a message to me, and Xiaobian will reply you in time. Thank you very much for your support of the developepaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!