A functional solution for implementing IP whitelists and blacklists using Security Options in .NET 8 #2313
Nikhil-nama
started this conversation in
Show and tell
Replies: 1 comment 1 reply
-
Hi Nikhil,
Very nice! I think these docs are useful to review: |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone 👋
Yesterday I faced an issue while trying to configure IP whitelist & blacklist with
SecurityOptions
in Ocelot v23.4.2 (.NET 8 backend).The configuration was not working as expected, but after some investigation, I found a working solution.
Add Security Options in Configuration file.
Step 1: Configure SecurityOptions in
ocelot.json
The SecurityOptions section in the Ocelot configuration file
ocelot.json
defines the IP whitelist and blacklist. Here's an updated example with additional notes for clarity:Key Points:
IPBlockedList
: Supports specific IPs (e.g.,192.168.0.10
) or CIDR ranges (e.g.,10.0.0.0/24
) for blocking entire subnets.IPAllowedList
: Lists trusted IPs or CIDR ranges. Including127.0.0.1
and::1
ensures localhost access (IPv4 and IPv6).ExcludeAllowedFromBlocked
: When true, IPs inIPAllowedList
are allowed even if they fall within a blocked range inIPBlockedList
. Set to false if blocked IPs should take precedence.Ensure the IPs are valid and correctly formatted to avoid runtime errors.
IPBlockedList
→ Add CIDR or specific IPs you want to block.IPAllowedList
→ Add the trusted IPs you want to whitelist.ExcludeAllowedFromBlocked
= true ensures allowed IPs override the blocked list.Step 2: Enable Forwarded Headers in "Program.cs"
Here’s an updated "Program.cs" with additional error handling and configuration:
With this setup, IP blocking and whitelisting started working correctly in my project 🎉
Hopefully this helps anyone else struggling with Security Options in the latest Ocelot. update this.
Beta Was this translation helpful? Give feedback.
All reactions