2024-10-03 20:14:04 +00:00
|
|
|
using System.Text;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2024-10-08 11:32:42 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-10-03 20:14:04 +00:00
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2024-10-08 11:32:42 +00:00
|
|
|
using Simple_API;
|
|
|
|
using Simple_API.Database;
|
2024-10-03 20:14:04 +00:00
|
|
|
|
|
|
|
//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.AddControllersWithViews();
|
2024-10-08 11:32:42 +00:00
|
|
|
builder.Services.AddControllers();
|
2024-10-01 16:50:41 +00:00
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
{
|
|
|
|
options.AddPolicy("AllowAllOrigins", corsBuilder =>
|
|
|
|
{
|
|
|
|
corsBuilder.AllowAnyOrigin()
|
|
|
|
.AllowAnyMethod()
|
|
|
|
.AllowAnyHeader();
|
|
|
|
});
|
|
|
|
});
|
2024-10-08 11:32:42 +00:00
|
|
|
builder.Services.AddDbContext<Database>(options =>
|
|
|
|
options.UseMySql(builder.Configuration.GetConnectionString("DATABASE"),
|
|
|
|
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("DATABASE"))));
|
|
|
|
builder.Services.AddScoped<UserService>();
|
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-08 11:32:42 +00:00
|
|
|
/* TODO : Uncomment if you're using nginx for https redirections.
|
|
|
|
Also ensure that your configuration looks like this :
|
|
|
|
|
|
|
|
server {
|
|
|
|
listen 443 ssl;
|
|
|
|
server_name your_domain.com;
|
|
|
|
|
|
|
|
ssl_certificate /path/to/localhost.pem;
|
|
|
|
ssl_certificate_key /path/to/localhost-key.pem;
|
|
|
|
|
|
|
|
location / {
|
|
|
|
proxy_pass http://localhost:your_backend_port; # Change this to your backend server
|
|
|
|
proxy_set_header Host $host;
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
|
|
|
{ ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto });
|
|
|
|
*/
|
|
|
|
|
2024-10-01 16:50:41 +00:00
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
}
|
|
|
|
|
2024-10-03 20:14:04 +00:00
|
|
|
app.UseAuthentication();
|
|
|
|
app.UseAuthorization();
|
2024-10-08 11:32:42 +00:00
|
|
|
app.UseMiddleware<Middlewares.ClaimsValidationMiddleware>();
|
2024-10-01 16:50:41 +00:00
|
|
|
app.UseCors("AllowAllOrigins");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.Run();
|