Banned IPs


List of examples:

Creating a new banned IP




// Creates a new banned IP object
BannedIPInfo bannedIP = new BannedIPInfo();

// Sets the banned IP's properties
bannedIP.IPAddress = "0.0.0.1";
bannedIP.IPAddressBanReason = "The IP has been banned.";
bannedIP.IPAddressAllowed = false;
bannedIP.IPAddressAllowOverride = false;
bannedIP.IPAddressBanType = BanControlEnum.AllNonComplete.ToStringRepresentation();
bannedIP.IPAddressBanEnabled = true;

// Saves the banned IP to the database
BannedIPInfoProvider.SetBannedIPInfo(bannedIP);


> Back to list of examples

Updating a banned IP




// Gets the banned IP from the database
BannedIPInfo bannedIP = BannedIPInfoProvider.GetBannedIPs()
                                            .WhereEquals("IPAddess", "0.0.0.1")
                                            .TopN(1) // Optimizes performance by limiting the amount of data transferred from the database server
                                            .FirstOrDefault();

// Updates the banned IP properties - bans only registrations from this IP address
bannedIP.IPAddressBanType = BanControlEnum.Registration.ToStringRepresentation();

// Saves the changes to the database
BannedIPInfoProvider.SetBannedIPInfo(bannedIP);


> Back to list of examples

Updating multiple banned IPs




// Gets all banned IPs whose address starts with '0.0.0'
var bannedIPs = BannedIPInfoProvider.GetBannedIPs().WhereStartsWith("BannedIP", "0.0.0.");

// Loops through individual banned IPs
foreach (BannedIPInfo bannedIP in bannedIPs)
{
    // Updates the banned IP properties - disables the ban
    bannedIP.IPAddressBanEnabled = false;

    // Saves the changes to the database
    BannedIPInfoProvider.SetBannedIPInfo(bannedIP);
}


> Back to list of examples

Checking if a user’s IP is banned




// Gets the IP of the current user
string checkedIP = RequestContext.UserHostAddress;

// Checks if the IP is allowed. Please note that this method also checks the current user's IP by default if you omit the first argument.
if (!BannedIPInfoProvider.IsAllowed(checkedIP, SiteContext.CurrentSiteName, BanControlEnum.AllNonComplete))
{
    // Do something (IP is banned)
}


> Back to list of examples

Deleting a banned IP




// Gets the banned IP
BannedIPInfo bannedIP = BannedIPInfoProvider.GetBannedIPs()
                                            .WhereEquals("IPAddess", "0.0.0.1")
                                            .TopN(1) // Optimizes performance by limiting the amount of data transferred from the database server
                                            .FirstOrDefault();

if (bannedIP != null)
{
    // Deletes the banned IP
    BannedIPInfoProvider.DeleteBannedIPInfo(bannedIP);
}


> Back to list of examples