/// <summary> /// Checks if an IP from the IpAddressCollection or the IpNetworkCollection is banned /// </summary> /// <param name="ipAddress">IP address</param> /// <returns>False or true</returns> public static bool IsIpAddressBanned(BannedIpAddress ipAddress) { // Check if the IP is valid if (!IsValidIp(ipAddress.Address.Trim())) { throw new NopException("The following isn't a valid IP address: " + ipAddress.Address); } // Check if the IP is in the banned IP addresses BannedIpAddressCollection ipAddressCollection = GetBannedIpAddressAll(); //if (ipAddressCollection.Contains(ipAddress)) foreach (BannedIpAddress ip in ipAddressCollection) { if (IsEqual(ipAddress.Address, ip.Address)) { return(true); } } // Check if the IP is in the banned IP networks BannedIpNetworkCollection ipNetworkCollection = GetBannedIpNetworkAll(); foreach (BannedIpNetwork ipNetwork in ipNetworkCollection) { // Get the first and last IPs in the network string[] rangeItem = ipNetwork.ToString().Split("-".ToCharArray()); // Get the exceptions as a list List <string> exceptionItem = new List <string>(); exceptionItem.AddRange(ipNetwork.IpException.ToString().Split(";".ToCharArray())); // Check if the IP is an exception if (exceptionItem.Contains(ipAddress.Address.ToString())) { return(false); } // Check if the 1st IP is valid if (!IsValidIp(rangeItem[0].Trim())) { throw new NopException("The following isn't a valid IP address: " + rangeItem[0]); } // Check if the 2nd IP is valid if (!IsValidIp(rangeItem[1].Trim())) { throw new NopException("The following isn't a valid IP address: " + rangeItem[1]); } //Check if the IP is in the given range if (IsGreaterOrEqual(ipAddress.Address.ToString(), rangeItem[0].Trim()) && IsLessOrEqual(ipAddress.Address.ToString(), rangeItem[1].Trim())) { return(true); } } // Return false otherwise return(false); }
private void BindGrid() { BannedIpAddressCollection ipAddressCollection = IpBlacklistManager.GetBannedIpAddressAll(); gvBannedIpAddress.DataSource = ipAddressCollection; gvBannedIpAddress.DataBind(); BannedIpNetworkCollection ipNetworkCollection = IpBlacklistManager.GetBannedIpNetworkAll(); gvBannedIpNetwork.DataSource = ipNetworkCollection; gvBannedIpNetwork.DataBind(); }
private static BannedIpNetworkCollection DBMapping(DBBannedIpNetworkCollection dbCollection) { if (dbCollection == null) { return(null); } var ipCollection = new BannedIpNetworkCollection(); foreach (var dbItem in dbCollection) { var item = DBMapping(dbItem); ipCollection.Add(item); } return(ipCollection); }
/// <summary> /// Gets all IP networks /// </summary> /// <returns>IP network collection</returns> public static BannedIpNetworkCollection GetBannedIpNetworkAll() { string key = BLACKLIST_ALLNETWORK_KEY; object obj2 = NopCache.Get(key); if (IpBlacklistManager.CacheEnabled && (obj2 != null)) { return((BannedIpNetworkCollection)obj2); } DBBannedIpNetworkCollection dbCollection = DBProviderManager <DBBlacklistProvider> .Provider.GetIpNetworkAll(); BannedIpNetworkCollection collection = DBMapping(dbCollection); if (IpBlacklistManager.CacheEnabled) { NopCache.Max(key, collection); } return(collection); }