确定请求是否来自本地网络(intranet)

本文关键字:本地网络 intranet 请求 是否 | 更新日期: 2023-09-27 18:22:17

我需要使用客户端或服务器端来确定请求是来自Internet还是Intranet。

我想解决的问题是:我们的网站可以从互联网和内联网访问。内联网用户(公司内部的用户)没有访问互联网的权限。我们使用Google Anylitics,当intranet用户访问该页面时,该页面需要很长时间才能上传,因为它试图下载从Google生成的(ga)JavaScript文件。

有什么解决方案吗?

确定请求是否来自本地网络(intranet)

您可以检查用户的ip地址。专用ip4地址总是以10、172或192.*开头…有关专用网络的更多信息,请点击此处。

您还可以使Google Analytics加载异步。

*****************更新-请阅读*****************************

正如@igor turman正确指出的那样,只有"172"answers"192"地址范围的一部分被指定为私人使用。以172和192开头的其余ip地址是公共的

以下是用于检查私有IP地址的正则表达式:

(^192'.168'.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])'.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])$)|(^172'.([1][6-9]|[2][0-9]|[3][0-1])'.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])'.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])$)|(^10'.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])'.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])'.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])$)

您可以在regexpal.com上测试此regex。

这就是我进行ip检查的方式:

string ipString = System.Web.HttpContext.Current.Request.UserHostAddress;
byte[] ipBytes = System.Net.IPAddress.Parse(ipString).GetAddressBytes();
int ip = System.BitConverter.ToInt32(ipBytes, 0);
// your network ip range
string ipStringFrom = "192.168.1.0";
byte[] ipBytesFrom = System.Net.IPAddress.Parse(ipStringFrom).GetAddressBytes();
int ipFrom = System.BitConverter.ToInt32(ipBytesFrom, 0);
string ipStringTo = "192.168.1.255";
byte[] ipBytesTo= System.Net.IPAddress.Parse(ipStringTo).GetAddressBytes();
int ipTo = System.BitConverter.ToInt32(ipBytesFrom, 0);
bool clientIsOnLAN = ipFrom >= ip && ip <= ipTo;

如果您有多个子网,只需对它们执行相同的操作(from、to),然后添加到上面的bool条件中我刚刚意识到,在你的情况下,以上可能有些过头了

或者,对你来说,它可能很简单:

bool isOnLAN = System.Web.HttpContext.Current.Request.UserHostAddress.StartsWith("192.168.1.")

亡灵巫师:
没有一个答案是好的或正确的
您可以将IP(仅IPv4-IPv6为UInt128)转换为UInt32值,然后您可以检查请求的IP是否在专用IP范围内:

例如,如果不是intranet,您可以使用它将cookie设置为"安全"。

For Each thisCookie As System.Web.HttpCookie In response.Cookies
    thisCookie.HttpOnly = True
    Dim ipString As String = System.Web.HttpContext.Current.Request.UserHostAddress
    If Not IPv4Info.IsPrivateIp(ipString) Then
        thisCookie.Secure = True
    End If
Next thisCookie

VB.NET类,您可以将其转换为C#(http://converter.telerik.com)

Public Class IPv4Info

    Private Class IPv4Range
        Public RangeStart As UInt32
        Public RangeEnd As UInt32
    End Class

    ' https://en.wikipedia.org/wiki/Private_network
    ' https://tools.ietf.org/html/rfc1918
    ' 192.168.0.0 - 192.168.255.255 (65,536 IP addresses)
    ' 172.16.0.0 - 172.31.255.255 (1,048,576 IP addresses)
    ' 10.0.0.0 - 10.255.255.255 (16,777,216 IP addresses)
    Private Shared Rng127 As IPv4Range = New IPv4Range() With {.RangeStart = GetIpNum("127.0.0.0"), .RangeEnd = GetIpNum("127.255.255.255")}
    Private Shared Rng192 As IPv4Range = New IPv4Range() With {.RangeStart = GetIpNum("192.168.0.0"), .RangeEnd = GetIpNum("192.168.255.255")} ' CIDR: 192.168.0.0/16 (255.255.0.0) 
    Private Shared Rng172 As IPv4Range = New IPv4Range() With {.RangeStart = GetIpNum("172.16.0.0"), .RangeEnd = GetIpNum("172.31.255.255")} ' CIDR: 172.16.0.0/12 (255.240.0.0)
    Private Shared Rng10 As IPv4Range = New IPv4Range() With {.RangeStart = GetIpNum("10.0.0.0"), .RangeEnd = GetIpNum("10.255.255.255")} ' CIDR: 10.0.0.0/8 (255.0.0.0)

    ' http://stackoverflow.com/questions/36831/how-do-you-parse-an-ip-address-string-to-a-uint-value-in-c
    Public Shared Function GetIpNum(ipString As String) As UInt32
        Dim ipAddress__1 As System.Net.IPAddress = System.Net.IPAddress.Parse("some.ip.address")
        Dim ipBytes As Byte() = ipAddress__1.GetAddressBytes()
        Dim ip As UInt32 = CUInt(ipBytes(0)) << 24
        ip += CUInt(ipBytes(1)) << 16
        ip += CUInt(ipBytes(2)) << 8
        ip += CUInt(ipBytes(3))
        Return ip
    End Function

    Public Shared Function isIn127(ipString As String) As Boolean
        Dim ip As UInt32 = GetIpNum(ipString)
        Return isIn127(ip)
    End Function

    Public Shared Function isIn127(x As UInt32) As Boolean
        If x >= Rng127.RangeStart AndAlso x <= Rng127.RangeEnd Then
            Return True
        End If
        Return False
    End Function

    Public Shared Function isIn192(ipString As String) As Boolean
        Dim ip As UInt32 = GetIpNum(ipString)
        Return isIn192(ip)
    End Function

    Public Shared Function isIn192(x As UInt32) As Boolean
        If x >= Rng192.RangeStart AndAlso x <= Rng192.RangeEnd Then
            Return True
        End If
        Return False
    End Function

    Public Shared Function isIn172(ipString As String) As Boolean
        Dim ip As UInt32 = GetIpNum(ipString)
        Return isIn172(ip)
    End Function

    Public Shared Function isIn172(x As UInt32) As Boolean
        If x >= Rng172.RangeStart AndAlso x <= Rng172.RangeEnd Then
            Return True
        End If
        Return False
    End Function

    Public Shared Function isIn10(ipString As String) As Boolean
        Dim ip As UInt32 = GetIpNum(ipString)
        Return isIn10(ip)
    End Function

    Public Shared Function isIn10(x As UInt32) As Boolean
        If x >= Rng10.RangeStart AndAlso x <= Rng10.RangeEnd Then
            Return True
        End If
        Return False
    End Function
    ' string ipString = System.Web.HttpContext.Current.Request.UserHostAddress;
    Public Shared Function IsPrivateIp(ipString As String) As Boolean
        Dim ip As UInt32 = GetIpNum(ipString)
        Return IsPrivateIp(ip)
    End Function

    Public Shared Function IsPrivateIp(ip As UInt32) As Boolean
        If isIn127(ip) OrElse isIn192(ip) OrElse isIn172(ip) OrElse isIn10(ip) Then
            Return True
        End If
        Return False
    End Function

End Class

注意:为了使用UInt128实现这一点,UInt128在NaCl.NET中有一个很好的实现。

使用Request.UserHostAddress属性来确定请求是来自公共网络还是专用网络