我们正在编写一项功能,允许管理员阻止/允许IP地址范围。
这对c#来说这很容易吗?
我正在考虑查看每个数字[这里]。[这里]。[这里]。[这里]然后将它与范围匹配,只是看看每个数字是否介于两者之间?
这适用于标准ip v4地址吗?
答案 0 :(得分:3)
我将它们转换为整数然后比较整数。但是什么是正确的取决于你如何定义范围。
UInt32 Ip4ToInt(string ip)
{
UInt32[] parts=ip.Split('.').Select(s=>UInt32.Parse(s)).ToArray();
if (parts.Length != 4)
throw new ArgumentException("InvalidIP");
return (parts[0]<<24) | (parts[1]<<16) | (parts[2]<<8) | parts[3];
}
例如1.1.1.99
应该成为范围1.1.1.1 - 1.1.2.2
的一部分吗?当比较每个组时,它不是,当比较整数时。
答案 1 :(得分:1)
我知道System.Net.IPAddress.Address
已弃用
但我认为这是最简单的方法:
bool result = false;
System.Net.IPAddress iStart = System.Net.IPAddress.Parse(ipStart);
System.Net.IPAddress iEnd = System.Net.IPAddress.Parse(ipEnd);
System.Net.IPAddress iIp = System.Net.IPAddress.Parse(ip);
if (iStart.Address <= iIp.Address && iEnd.Address >= iIp.Address)
{
result = true;
}
PS。 ipStart,ipEnd,ip是字符串。示例:127.0.0.1
答案 2 :(得分:0)
public static bool CheckIfIpValid(string allowedStartIp, string allowedEndIp, string ip)
{
// if both start and end ip's are null, every user with these credential can log in, no ip restriction needed.
if (string.IsNullOrEmpty(allowedStartIp) && string.IsNullOrEmpty(allowedEndIp))
return true;
bool isStartNull = string.IsNullOrEmpty(allowedStartIp),
isEndNull = string.IsNullOrEmpty(allowedEndIp);
string[] startIpBlocks, endIpBlocks, userIp = ip.Split('.');
if (!string.IsNullOrEmpty(allowedStartIp))
startIpBlocks = allowedStartIp.Split('.');
else
startIpBlocks = "0.0.0.0".Split('.');
if (!string.IsNullOrEmpty(allowedEndIp))
endIpBlocks = allowedEndIp.Split('.');
else
endIpBlocks = "999.999.999.999".Split('.');
for (int i = 0; i < userIp.Length; i++)
{
// if current block is smaller than allowed minimum, ip is not valid.
if (Convert.ToInt32(userIp[i]) < Convert.ToInt32(startIpBlocks[i]))
return false;
// if current block is greater than allowed maximum, ip is not valid.
else if (Convert.ToInt32(userIp[i]) > Convert.ToInt32(endIpBlocks[i]))
return false;
// if current block is greater than allowed minimum, ip is valid.
else if ((Convert.ToInt32(userIp[i]) > Convert.ToInt32(startIpBlocks[i])) && !isStartNull)
return true;
}
return true;
}
答案 3 :(得分:0)
最近,我需要在ASP.NET Core中执行类似的操作。我采用了使用System.Net.IPAddress
将IP地址表示为byte
数组的方法。然后,我比较了数组中的元素,以查看主题IP地址是否在范围内。这与CodesInChaos和Pabuc给出的答案类似。
System.Net.IPAddress.TryParse(ipAddressRule.StartIpAddress, out var startIpAddress);
System.Net.IPAddress.TryParse(ipAddressRule.EndIpAddress, out var endIpAddress);
if (IsWithinAllowedIpAddresses(clientIpAddress.GetAddressBytes(), startIpAddress.GetAddressBytes(), endIpAddress.GetAddressBytes()))
{
...
}
public bool IsWithinAllowedIpAddresses(byte[] ipAddress, byte[] startAllowIpAddress, byte[] endAllowIpAddress)
{
if (ipAddress.Length != startAllowIpAddress.Length)
{
throw new ArgumentException();
}
if (ipAddress.Length != endAllowIpAddress.Length)
{
throw new ArgumentException();
}
for (int i = 0; i < ipAddress.Length; i++)
{
if (ipAddress[i] < startAllowIpAddress[I])
{
return false;
}
if (ipAddress[i] > endAllowIpAddress[I])
{
return false;
}
}
return true;
}