0

I am excluding some of the ipaddress sending mail from my application.Here i created the custom tags in web.config file

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

I had created the section ExcludeIPAddressListSectionGroup and given range to exclude those ips

Am reading the custom tags from web.config like this

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

I need to format the string and pass startingip and ending ip to this function bool Between(long value, long left, long right)

fero
  • 6,050
  • 1
  • 33
  • 56
user2211918
  • 91
  • 3
  • 14

1 Answers1

0

Use following code. It works for 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));
mit
  • 1,763
  • 4
  • 16
  • 27
  • Is it possible to split both '; =' delimiter in a single statement to get the starting and ending ip address. – user2211918 Oct 08 '13 at 10:08
  • The best way is to use Regex, if your config Range string will be not changed in future. Please see this link ( http://stackoverflow.com/questions/15667927/how-to-keep-the-delimiters-of-regex-split) for regex split exmple but that does match your exact requirement, you need to do little bit r&d yourself. – mit Oct 08 '13 at 10:25
  • @user2211918 if you're happy with the answer, mark it as answered. You've asked 5 questions with 0 marked answered. – Mikael Östberg Oct 08 '13 at 10:33
  • I am marking the question as answer but its giving the message as you need 15 reputation to mark the answer – user2211918 Oct 08 '13 at 10:51