0

我排除了一些从我的应用程序发送邮件的 ipaddress。这里我在 web.config 文件中创建了自定义标签

<configSections>
  <sectionGroup name="ExcludeIPAddressListSectionGroup">
    <section name="IPAddressListSection" type="CustomConfigurationHandler.CustomConfiguration, CustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere"/>
  </sectionGroup>
</configSections>

<ExcludeIPAddressListSectionGroup>
  <IPAddressListSection>
      <ExcludedList1 Range="StartingIP=192.168.185.1; EndingIP=192.168.185.50" ></ExcludedList1>
      <ExcludedList2 Range="StartingIP=192.168.185.51;EndingIP=192.168.185.51" ></ExcludedList2>
  </IPAddressListSection>
</ExcludeIPAddressListSectionGroup>

我创建了 ExcludeIPAddressListSectionGroup 部分并给出了排除这些 ips 的范围

我正在像这样从 web.config 读取自定义标签

 Hashtable config = (Hashtable)ConfigurationManager.GetSection("ExcludeIPAddressListSectionGroup/IPAddressListSection");
 foreach (DictionaryEntry deKey in config)
 {
     Hashtable attribs = (Hashtable)deKey.Value;
     foreach (DictionaryEntry deAttrib in attribs)
     {
         string range = deAttrib.Value.ToString();
         string[] range_arr = range.Split(';');

         foreach (string range_str in range_arr)
         {
             Response.Write(range_str+"<br>");
         }
     }
 }

我需要格式化字符串并将startingip和ending ip传递给这个函数bool Between(long value, long left, long right)

4

1 回答 1

0

使用以下代码。它适用于 IPV4。

        List<string> lstIPValues = new List<string>();
        foreach (string range_str in range_arr)
        {
             //again split to get ragne value into List of string.
            string[] ipValues = range_str.Split('=');
            lstIPValues.Add(ipValues[1]);    
        }
        Between(valueToBeChecked,, BitConverter.ToInt64(IPAddress.Parse(lstIPValues[0]).GetAddressBytes(), 0), BitConverter.ToInt64(IPAddress.Parse(lstIPValues[1]).GetAddressBytes(), 0));
于 2013-10-08T09:28:04.453 回答