2024-10-03 20:14:04 +00:00
|
|
|
using System.Text;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
|
|
|
//Builder configuration
|
2024-10-01 16:50:41 +00:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
builder.Services.AddSwaggerGen();
|
2024-10-03 20:14:04 +00:00
|
|
|
builder.Services.AddControllers();
|
|
|
|
builder.Services.AddControllersWithViews();
|
2024-10-01 16:50:41 +00:00
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
{
|
|
|
|
options.AddPolicy("AllowAllOrigins", corsBuilder =>
|
|
|
|
{
|
|
|
|
corsBuilder.AllowAnyOrigin()
|
|
|
|
.AllowAnyMethod()
|
|
|
|
.AllowAnyHeader();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-10-03 20:14:04 +00:00
|
|
|
|
|
|
|
// JWT Configuration
|
|
|
|
var jwtSettings = builder.Configuration.GetSection("Jwt");
|
|
|
|
var key = jwtSettings["Key"];
|
|
|
|
var issuer = jwtSettings["Issuer"];
|
|
|
|
var audience = jwtSettings["Audience"];
|
|
|
|
if (string.IsNullOrEmpty(key))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
|
|
{
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
})
|
|
|
|
.AddJwtBearer(options =>
|
|
|
|
{
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
{
|
|
|
|
ValidateIssuer = true,
|
|
|
|
ValidateAudience = true,
|
|
|
|
ValidateLifetime = true,
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
ValidIssuer = issuer,
|
|
|
|
ValidAudience = audience,
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Build
|
2024-10-01 16:50:41 +00:00
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
2024-10-03 20:14:04 +00:00
|
|
|
|
|
|
|
// App configuration
|
|
|
|
app.MapControllers();
|
|
|
|
|
2024-10-01 16:50:41 +00:00
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
2024-10-03 20:14:04 +00:00
|
|
|
app.UseAuthentication();
|
|
|
|
app.UseAuthorization();
|
2024-10-01 16:50:41 +00:00
|
|
|
app.UseCors("AllowAllOrigins");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.Run();
|