2013-10-08 42 views
0

我排除一些ip地址從我application.Here發送郵件的我創建的自定義標籤從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和web.config文件給定範圍內,以排除那些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和e發送ip到這個函數bool Between(long value,long left,long right)

+0

我已經dited你的問題因爲web.config對用戶不可見plz檢查它 – Neel

+5

你的問題是什麼? –

回答

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)); 
+0

ya其工作感謝 – user2211918

+0

是否有可能分裂'; ='分隔符在單個語句中獲取起始和結束IP地址。 – user2211918

+0

最好的方法是使用正則表達式,如果你的配置範圍字符串將來不會改變。請看這個鏈接(http://stackoverflow.com/questions/15667927/how-to-keep-the-delimiters-of-regex-split)正則表達式分裂實例,但這確實符合你的確切要求,你需要做的很少自己研發一下。 – mit