---
title: Banned IPs
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

List of examples:

### Creating a new banned IP

```csharp

// 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 = BannedIPInfoProvider.BanControlEnumString(BanControlEnum.AllNonComplete);
bannedIP.IPAddressBanEnabled = true;

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

```

[> Back to list of examples](#bannedips-toc)

### Updating a banned IP

```csharp

// 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
                                            .FirstObject; // Ensures conversion to the BannedIPInfo type

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

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

```

[> Back to list of examples](#bannedips-toc)

### Updating multiple banned IPs

```csharp

// 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](#bannedips-toc)

### Checking if a user's IP is banned

```csharp

// 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](#bannedips-toc)

### Deleting a banned IP

```csharp

// 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
                                            .FirstObject; // Ensures conversion to the BannedIPInfo type

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

```

[> Back to list of examples](#bannedips-toc)
