newIp.IPAddressBanEnabled = true; // Save the banned IP BannedIPInfoProvider.SetBannedIPInfo(newIp); }
The following example gets and updates a banned IP.
privatebool GetAndUpdateBannedIp() { // Prepare the parameter string where = "IPAddress LIKE N'MyNewIp'"; //Get the data DataSet ips = BannedIPInfoProvider.GetBannedIPs(where, null); if (!DataHelper.DataSourceIsEmpty(ips)) { // Get the first banned ip BannedIPInfo modifyIp = newBannedIPInfo(ips.Tables[0].Rows[0]); // Update the properties modifyIp.IPAddress = modifyIp.IPAddress.ToLower(); // Save the changes BannedIPInfoProvider.SetBannedIPInfo(modifyIp); returntrue; } returnfalse; }
The following example gets and bulk updates banned IPs.
privatebool GetAndBulkUpdateBannedIps() { // Prepare the parameters string where = "IPAddress LIKE N'MyNewIp%'"; // Get the data DataSet ips = BannedIPInfoProvider.GetBannedIPs(where, null); if (!DataHelper.DataSourceIsEmpty(ips)) { // Loop through the individual items foreach (DataRow ipDr in ips.Tables[0].Rows) { // Create object from DataRow BannedIPInfo modifyIp = newBannedIPInfo(ipDr); // Update the properties modifyIp.IPAddress = modifyIp.IPAddress.ToUpper(); // Save the changes BannedIPInfoProvider.SetBannedIPInfo(modifyIp); } returntrue; } returnfalse; }
The following example removes a banned IP.
privatebool DeleteBannedIp() { string where = "IPAddress LIKE N'MyNewIp%'"; // Get DataSet DataSet ips = BannedIPInfoProvider.GetBannedIPs(where, null); if (!DataHelper.DataSourceIsEmpty(ips)) { // Get the first banned ip BannedIPInfo deleteIp = newBannedIPInfo(ips.Tables[0].Rows[0]); // Delete the banned ip BannedIPInfoProvider.DeleteBannedIPInfo(deleteIp); returntrue; } returnfalse; }
The method in the following example returns true if the specified IP address is banned for the current site, otherwise it returns false.
privatebool CheckBannedIp() { // Prepare the parameter string where = "IPAddress LIKE N'MyNewIp'"; // Get DataSet DataSet ips = BannedIPInfoProvider.GetBannedIPs(where, null); if (!DataHelper.DataSourceIsEmpty(ips)) { // Get the first banned ip BannedIPInfo checkIp = newBannedIPInfo(ips.Tables[0].Rows[0]);
if (!BannedIPInfoProvider.IsAllowed(checkIp.IPAddress, CMSContext.CurrentSiteName, BanControlEnum.AllNonComplete))