Skip to content

Instantly share code, notes, and snippets.

@SibeeshVenu
Created August 11, 2023 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SibeeshVenu/a857e6b16005eb36b8161bfc000fdaf8 to your computer and use it in GitHub Desktop.
Save SibeeshVenu/a857e6b16005eb36b8161bfc000fdaf8 to your computer and use it in GitHub Desktop.
Program file with AddHostedService<IpHostedService>()
using DotNetIpFilter.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IIpFilterService, IpFilterService>();
// To add the IP addresses to filter
builder.Services.AddHostedService<IpHostedService>();
// Enable the Ip Action Filter with Admin Safe IP List
builder.Services.AddScoped(container =>
{
var loggerFactory = container.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<IpActionFilter>();
return new IpActionFilter(builder.Configuration["AdminSafeIpList"], logger);
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment