using System; using System.Linq; using System.Net; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; namespace HRM.DA { /// /// Who is on the other end of the current HTTP request: IP address, device id /// (mobile / tab) and computer name. /// /// Services in HRM.DA are created with new, not through DI, so they read the /// request through the one that Startup hands to /// . Read it on the request thread only: once the request /// has finished (e.g. inside a background save thread) there is no context left. /// /// Note Environment.MachineName / Dns.GetHostName() are the name of /// the web SERVER, never the user's computer. /// public static class ClientRequestInfo { /// Headers a mobile / tab app may send its device id in (first one found wins). private static readonly string[] DeviceIdHeaders = { "X-Device-Id", "DeviceId", "DeviceID", "Device-Id" }; /// A reverse-DNS lookup that has no answer can hang for seconds; never wait longer than this. private static readonly TimeSpan HostLookupTimeout = TimeSpan.FromSeconds(2); private static IHttpContextAccessor _accessor; /// Called once from Startup.Configure. public static void Configure(IHttpContextAccessor accessor) { _accessor = accessor; } /// The current request, or null outside a request (background thread, console job). private static HttpContext Current => _accessor?.HttpContext; /// /// The client's IP: the first X-Forwarded-For entry when behind a proxy / load balancer /// (the same rule as the login rate limiter in Startup), else the socket's remote address. /// public static string GetIPAddress() { HttpContext context = Current; if (context == null) return null; string forwarded = context.Request.Headers["X-Forwarded-For"].FirstOrDefault(); if (!string.IsNullOrWhiteSpace(forwarded)) { string first = forwarded.Split(',')[0].Trim(); if (first != string.Empty) return first; } IPAddress remote = context.Connection.RemoteIpAddress; if (remote == null) return null; if (remote.IsIPv4MappedToIPv6) remote = remote.MapToIPv4(); return remote.Equals(IPAddress.IPv6Loopback) ? IPAddress.Loopback.ToString() // "::1" -> "127.0.0.1" : remote.ToString(); } /// The device id a mobile / tab app sent as a request header, or null (browser). public static string GetDeviceId() { HttpContext context = Current; if (context == null) return null; foreach (string header in DeviceIdHeaders) { string value = context.Request.Headers[header].FirstOrDefault(); if (!string.IsNullOrWhiteSpace(value)) return value.Trim(); } return null; } /// /// The client's computer name by reverse DNS of its IP. Works on a company network /// whose DNS registers workstations; otherwise (internet, mobile data) there is no /// name and the IP itself is returned. Blocks for at most , /// so call it off the request thread where possible. /// public static string ResolveComputerName(string ipAddress) { if (string.IsNullOrWhiteSpace(ipAddress) || !IPAddress.TryParse(ipAddress, out IPAddress ip)) return ipAddress; if (IPAddress.IsLoopback(ip)) return Environment.MachineName; // the user is on the server itself try { Task lookup = Dns.GetHostEntryAsync(ip); if (lookup.Wait(HostLookupTimeout) && !string.IsNullOrWhiteSpace(lookup.Result?.HostName)) return lookup.Result.HostName; } catch (Exception) { // No PTR record / DNS unreachable: fall back to the IP. } return ipAddress; } } }