Adding security
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
using LCU.Library.Internal.ApiSecurity;
|
||||
using LCU.Library.Internal.ApiSecurity.Constants;
|
||||
using LCU.Library.Internal.ApiSecurity.Middleware;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers()
|
||||
.AddNewtonsoftJson(options =>
|
||||
{
|
||||
options.SerializerSettings.Converters.Add(new StringEnumConverter());
|
||||
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
|
||||
});
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo
|
||||
{
|
||||
Title = "IntegrationTestApiTwo",
|
||||
Version = "v1"
|
||||
});
|
||||
|
||||
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||
{
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Id = HeaderKeys.ApiKey, // Must match the "AddSecurityDefinition"
|
||||
Type = ReferenceType.SecurityScheme
|
||||
}
|
||||
},
|
||||
new List<string>()
|
||||
}
|
||||
});
|
||||
|
||||
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||
{
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Id = HeaderKeys.Client, // Must match the "AddSecurityDefinition"
|
||||
Type = ReferenceType.SecurityScheme
|
||||
}
|
||||
},
|
||||
new List<string>()
|
||||
}
|
||||
});
|
||||
|
||||
c.AddSecurityDefinition(HeaderKeys.ApiKey,
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Description = HeaderKeys.ApiKey,
|
||||
In = ParameterLocation.Header,
|
||||
Name = HeaderKeys.ApiKey,
|
||||
Type = SecuritySchemeType.ApiKey,
|
||||
Scheme = "apikey"
|
||||
});
|
||||
|
||||
c.AddSecurityDefinition(HeaderKeys.Client,
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Description = HeaderKeys.Client,
|
||||
In = ParameterLocation.Header,
|
||||
Name = HeaderKeys.Client,
|
||||
Type = SecuritySchemeType.ApiKey,
|
||||
Scheme = "apikey"
|
||||
});
|
||||
});
|
||||
|
||||
builder.Services.AddApiKeySecurity(builder.Configuration);
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c => c.SwaggerEndpoint("v1/swagger.json", "IntegrationTestApiTwo"));
|
||||
}
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseMiddleware<ApiKeyMiddleware>();
|
||||
|
||||
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user