Necromancing:Ни один из ответов не является хорошим или правильным.Вы можете преобразовать IP (только IPv4 - IPv6 - UInt128) в значение UInt32, а затем вы можете проверить, находится ли запрашивающий IP где-то в частных диапазонах IP-адресов:
, например, вы можете использовать его для установки файлов 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)
' /11544/kak-vy-analiziruete-stroku-ip-adresa-v-znachenie-uint-v-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.