Kentico CMS 7.0 Developer's Guide

Managing banned IPs

Managing banned IPs

Previous topic Next topic Mail us feedback on this topic!  

Managing banned IPs

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example adds a banned IP.

 

private void CreateBannedIp()
{
    // Create new banned ip object
    BannedIPInfo newIp = new BannedIPInfo();
 
    // Set the properties
    newIp.IPAddress = "MyNewIp";
    newIp.IPAddressBanReason = "Ban reason";
    newIp.IPAddressAllowed = true;
    newIp.IPAddressAllowOverride = true;

   newIp.IPAddressBanType = BannedIPInfoProvider.BanControlEnumString(BanControlEnum.AllNonComplete);

    newIp.IPAddressBanEnabled = true;
        
    // Save the banned IP
    BannedIPInfoProvider.SetBannedIPInfo(newIp);
}

 

The following example gets and updates a banned IP.

 

private bool 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 = new BannedIPInfo(ips.Tables[0].Rows[0]);
            
        // Update the properties
        modifyIp.IPAddress = modifyIp.IPAddress.ToLower();
 
        // Save the changes
        BannedIPInfoProvider.SetBannedIPInfo(modifyIp);
 
        return true;
    }
 
    return false;
}

 

The following example gets and bulk updates banned IPs.

 

private bool 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 = new BannedIPInfo(ipDr);
 
            // Update the properties
            modifyIp.IPAddress = modifyIp.IPAddress.ToUpper();
 
            // Save the changes
            BannedIPInfoProvider.SetBannedIPInfo(modifyIp);
        }
 
        return true;
    }
 
    return false;
}

 

The following example removes a banned IP.

 

private bool 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 = new BannedIPInfo(ips.Tables[0].Rows[0]);
 
        // Delete the banned ip
        BannedIPInfoProvider.DeleteBannedIPInfo(deleteIp);
 
        return true;
    }
 
    return false;
}

 

The method in the following example returns true if the specified IP address is banned for the current site, otherwise it returns false.

 

private bool 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 = new BannedIPInfo(ips.Tables[0].Rows[0]);

 
      if (!BannedIPInfoProvider.IsAllowed(checkIp.IPAddress, CMSContext.CurrentSiteName, BanControlEnum.AllNonComplete))

        {
            return true;
        }
    }
 
    return false;
}