English 中文(简体)
对照使用C#的多种价值检查ip地址的最佳方式
原标题:Easiest way to check ip address against a range of values using c#
  • 时间:2010-12-14 11:13:51
  •  标签:
  • c#
  • asp.net
  • ip

我们撰写了一份内容,使一名管理人能够阻挡/限制排位。

这是否容易与否?

我正在考虑研究每一数字[范围]。

标准ip v4的工作是否涉及?

最佳回答

I d 将其转换为ger,然后比较 in。 但正确之处取决于你如何界定范围。

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.2.2的一部分? 在比较每个组别时,比较分类时是t。

问题回答
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;
    }

I know that System.Net.IPAddress.Address is deprecated But I think that this is the easiest way:

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;
}

页: 1 例如:127.0.0.1

最近,我需要在伙伴关系中做类似的事情。 NET Core。 我采用了<代码>System.Net.IPAddress的做法,将IP地址作为byte阵列。 然后,我比较各阵列中的内容,看IP的主题地址是否属于范围。 这是《InChaos和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;
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签