title: IPFiltering Asp Net Core
body: <p>I'm using the following nuget package : <a href="https://github.com/msmolka/ZNetCS.AspNetCore.IPFiltering" rel="nofollow noreferrer">https://github.com/msmolka/ZNetCS.AspNetCore.IPFiltering</a></p>
  
  <p>I'm using it to block IP that are trying to bruteforce the authentication of my app.
  The Blacklist is defined in the appsetting.json, and I don't know how to dynamicaly modify it during runtime, for example, add an IP that has a bad password.</p>
  
  <p>The way I'm actually doing isn't working as I still can connect even if the IP is correctly persisted in the conf ...</p>
  
  <p>Startup.cs</p>
  
  <pre><code>namespace Sondage
  {
  public class Startup
  {
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
      }
  
      public IConfiguration Configuration { get; }
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {
  
          services.AddIPFiltering(this.Configuration.GetSection("IPFiltering"));
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
          else
          {
              app.UseHsts();
          }
          app.UseIPFiltering();
          app.UseHttpsRedirection();
          app.UseMvc();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>appsettings.json</p>
  
  <pre><code>{
      "Logging": {
         "LogLevel": {
            "Default": "Warning"
          } 
       },
       "AllowedHosts": "*",
  
       "IPFiltering": {
       "DefaultBlockLevel": "None",
       "HttpStatusCode": 404,
       "Blacklist": [],
       "IgnoredPaths": [ "GET:/ignoreget", "*:/ignore" ]
   }
  </code></pre>
  
  <p>}</p>
  
  <p>Part of my controller :</p>
  
  <pre><code>[Route("api/authenticate")]
  [ApiController]
  public class authenticationController : ControllerBase
  {
  
      private IConfiguration _configuration;
  
      public authenticationController(IConfiguration Configuration)
      {
          _configuration = Configuration;
      }
  
      [HttpPost]
      public string authent(string value)
      {
          Dictionary&lt;string, string&gt; result = new Dictionary&lt;string, string&gt;();
          IPAddress ip_addr = HttpContext.Connection.RemoteIpAddress;
  
          if(!Globals.tryByIP.TryGetValue(ip_addr, out int numberOfTry)) {
              Globals.tryByIP.Add(ip_addr, 0);
          } else
          {
              if(numberOfTry&gt;=0)
              {
                  Console.WriteLine("-----");
                  _configuration.GetSection("IPFiltering")["Blacklist"] = ip_addr.ToString();
                  Console.WriteLine(_configuration.GetSection("IPFiltering")["Blacklist"]);
              }
          }
  </code></pre>