Extensions: implemented GetLocalIpAddress().

This commit is contained in:
Shreyas Zare
2023-10-29 19:12:24 +05:30
parent bee4aaeffd
commit 1dba38fb69

View File

@@ -61,6 +61,25 @@ namespace DnsServerCore
} }
} }
public static IPAddress GetLocalIpAddress(this HttpContext context)
{
try
{
IPAddress localIP = context.Connection.LocalIpAddress;
if (localIP is null)
return IPAddress.Any;
if (localIP.IsIPv4MappedToIPv6)
localIP = localIP.MapToIPv4();
return localIP;
}
catch
{
return IPAddress.Any;
}
}
public static UserSession GetCurrentSession(this HttpContext context) public static UserSession GetCurrentSession(this HttpContext context)
{ {
if (context.Items["session"] is UserSession userSession) if (context.Items["session"] is UserSession userSession)