32 lines
946 B
C#
32 lines
946 B
C#
using System.Net;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
//https://stackoverflow.com/a/41242493/8939
|
|
public static class IsLocalExtension
|
|
{
|
|
|
|
private const string NullIpAddress = "::1";
|
|
|
|
public static bool IsLocal(this HttpRequest req)
|
|
{
|
|
var connection = req.HttpContext.Connection;
|
|
if (connection.RemoteIpAddress.IsSet())
|
|
{
|
|
//We have a remote address set up
|
|
return connection.LocalIpAddress.IsSet()
|
|
//Is local is same as remote, then we are local
|
|
? connection.RemoteIpAddress.Equals(connection.LocalIpAddress)
|
|
//else we are remote if the remote IP address is not a loopback address
|
|
: IPAddress.IsLoopback(connection.RemoteIpAddress);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private static bool IsSet(this IPAddress address)
|
|
{
|
|
return address != null && address.ToString() != NullIpAddress;
|
|
}
|
|
}
|