by Keyvan Nayyeri via Keyvan Nayyeri on 6/11/2007 5:18:53 PM
IP banning is a commercial feature of Community Server which is available in commercial versions (I don't know if it's still a commercial feature in 2007 or not). However, the topic of this book is working with IP banning features in Community Server API and code.
IP banning has its own namespace located at CommunityServer.IPBanning and all IP banning classes are located at CommunityServer.IPBanning.Components. As a background, you have to know that it's possible to block a single IP or a range of IPs in Community Server control panel. A single banned IP is named address and a range of IPs is named network in Community Server API. IP banning, itself, is a CSModule that is located at CommunityServer.IPBanning.Components.IPBanningModule.
There are two classes named BannedAddress and BannedNetwork (the second one is derived from first one) to represent single banned IP and a range of banned IPs in API. Both these classes share a read only Key property as their identifier.
There is also an IPBanning helper class which lets you to get an array of banned addresses or networks or check to see if a System.Net.IPAddress is banned or not.
IPBanningData is a method factory that provides some static methods to add, delete or fetch banned addresses and networks or get a merged list of banned addresses and networks as an ArrayList.
IPNetwork class gives some helper methods to work with a range of IPs. Some of these methods helps to get the class of an IP as a CommunityServer.IPBanning.Components.NetworkClass or check to see if an address is in the range of a network.
Let's discover a few examples.
In the first example, I create a BannedAddress object based on a passed IP string as parameter to add it to banned list with IPBanningData.AddBannedAddress() method.
void AddBannedIP(string IP)
{
BannedAddress ba = new BannedAddress();
ba.Address = IP;
ba.Date = DateTime.Now;
ba.SettingsID = CSContext.Current.SettingsID;
IPBanningData.AddBannedAddress(ba);
}
AreBanned() is a method to check if a list of IPAddress objects are banned or not. It iterates through the list of IPs and uses IPBanning.IsBanned() method to check if all IPs are banned or not.
bool AreBanned(List<IPAddress> IPs)
foreach (IPAddress IP in IPs)
if (!IPBanning.Instance().IsBanned(IP))
return false;
return true;
In the last example, IsBannedByRange() method gets three IPs and checks if the third IP is blocked by the range of first and second IPs. It uses the IPNetwork object and its IsAddressInNetwork() method to accomplish this.
bool IsBannedByRange(IPAddress lower, IPAddress upper, IPAddress IP)
IPNetwork network = new IPNetwork(lower, upper);
if (network.IsAddressInNetwork(IP))
else
Original Post: CS Dev Guide: IP Banning
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.