Is there native .NET type for CIDR subnets?

后端 未结 3 1551
陌清茗
陌清茗 2021-02-07 06:10

It\'s simple enough to code up a class to store/validate something like 192.168.0.0/16, but I was curious if a native type for this already existed in .NET? I woul

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-07 07:02

    For ASP.NET Core there is now IPNetwork class. Example usage below:

    var addr = IPAddress.Parse("192.168.0.0");
    var mask = 16;
    var test = new IPNetwork(addr, mask).Contains(context.Connection.RemoteIpAddress);
    

提交回复
热议问题