我需要使用客户端或服务器端来识别来自Internet或Intranet的请求。
我想解决的问题是:我们的网站可以从互联网和内部网访问。 Intranet用户(公司内部的用户)无法访问Internet。我们正在使用Google Anylitics,当Intranet用户访问该页面时,该页面需要很长时间才能上传,因为它会尝试下载(ga)从Google生成的JavaScript文件。
任何解决方案?
答案 0 :(得分:13)
您可以检查用户的IP地址。私有IP4地址始终以10. ,或172。或192. * ...私有网络上的更多信息here开头。
您也可以make Google Analytics load Asynchronous。
*****************更新 - 请仔细阅读************************** ***********
正如@ igor-turman正确指出的那样,只有“172”和“192”地址范围的一部分被指定供私人使用。 以172和192开头的剩余IP地址为PUBLIC。
以下是检查私有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 here上测试此正则表达式。
答案 1 :(得分:4)
我将如何进行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;
如果您有多个子网,只需为它们(从,到)执行相同操作,然后添加到上面的bool条件。 我刚才意识到,在你的情况下,上述情况可能有点矫枉过正。
或者,对你来说,它可能很简单:
bool isOnLAN = System.Web.HttpContext.Current.Request.UserHostAddress.StartsWith("192.168.1.")
答案 2 :(得分:1)
Necromancing:
没有一个答案是好的或正确的
您可以将IP(仅限IPv4 - IPv6是UInt128)转换为UInt32值,然后您可以检查请求IP是否位于私有IP范围内的某个位置:
e.g。如果它不是内联网,您可以使用它将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 in NaCl.NET.
答案 3 :(得分:0)
使用Request.UserHostAddress属性确定请求是来自公共网络还是专用网络