79473674

Date: 2025-02-27 19:05:04
Score: 1.5
Natty:
Report link

Copied from: https://stackoverflow.com/a/79473620/8216122 simply call: string sIP = await GetClientIP.ClientIP();

using System.Net.Sockets;
using System.Net;
    
public static class GetClientIP
{
    private static HttpContext _httpContext => new HttpContextAccessor().HttpContext;
    public static async Task<string> ClientIP()
    {
        string sClientIP = String.Empty;
        IPAddress? ip = _httpContext.Connection.RemoteIpAddress;
        if (ip != null) {
            if (ip.AddressFamily == AddressFamily.InterNetworkV6) {
                ip = Dns.GetHostEntry(ip).AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);
            }
            sClientIP = ip.ToString();
        }
        return sClientIP;
    }
}
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Richard Mneyan