Determine if a request is coming from the local network (intranet)

I need to determine if the request comes from the Internet or Intranet, using either the client or server side.

The problem I'm trying to solve is access to our website from the Internet and intranet. An intranet user (a user within the company) does not have access to the Internet. We use Google Anylitics, when an intranet user accesses a page, the page loads so long that it tries to load a (ga) JavaScript file created by Google.

Any solution?

+7
source share
4 answers

You can check the IP address of the user. A private ip4 address always starts with 10., or 172. or 192. * ... more information about private networks here .

You can also make the Google Analytics download asynchronous .

***************** UPDATE - PLEASE READ *************************** ** *********

As @ igor-turman rightly pointed out, only the private part of the address ranges "172" and "192" is for private use. The remaining ip addresses starting with 172 and 192 ARE PUBLIC.

Here is a regex expression for checking private IP addresses:

(^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])$) 

You can check out this regex at regexpal.com here .

+12
source

Here is how I would do an ip check:

 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; 

If you have several subnets, just do the same for them (from, to), and then add the bool condition above. I just realized that in your case the above may be redundant.

Also, for you, it could be simple:

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

Necromancers:
None of the answers are good or right.
You can convert IP (IPv4 only - IPv6 is UInt128) to a UInt32 value, and then you can check if the requesting IP address is in private IP ranges:

eg. you can use this to set cookies to "Secure" if it is not an intranet.

 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 Class, which you can convert to C # yourself ( 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 

Note. For this, with UInt128 there is a good implementation of UInt128 in NaCl.NET.

+2
source

Use the Request.UserHostAddress property to determine if the request came from a public or private network.

0
source

All Articles