129 lines
4.1 KiB
C#
129 lines
4.1 KiB
C#
using Core_Archi.Helpers;
|
|
using AuthHex;
|
|
using AuthHex.Infra.UoW;
|
|
using AuthHex.Interfaces;
|
|
using AuthHex.Repos;
|
|
using AuthHex.Services.UserManager;
|
|
using AuthHex.Services.RecoveryManager;
|
|
using AuthHex.Utility;
|
|
using AuthSystem.API.Utils;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Text;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
builder.Services.AddDbContext<AppDBContext>(options =>
|
|
{
|
|
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
|
options.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 21)));
|
|
});
|
|
|
|
// Configure RSA Key Provider
|
|
var privateKeyXml = builder.Configuration["Jwt:RsaPrivateKey"]!;
|
|
var rsaKeyProvider = new RsaKeyProvider(privateKeyXml);
|
|
builder.Services.AddSingleton(rsaKeyProvider);
|
|
|
|
// Add JWT Authentication
|
|
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 = builder.Configuration["Jwt:Issuer"],
|
|
ValidAudience = builder.Configuration["Jwt:Audience"],
|
|
IssuerSigningKey = rsaKeyProvider.GetPublicKey(),
|
|
ClockSkew = TimeSpan.Zero
|
|
};
|
|
});
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
builder.Services.AddScoped<IUnitOfWork, EFUnitOfWork>();
|
|
builder.Services.AddScoped<IUserManageRepository, UserManageRepository>();
|
|
builder.Services.AddScoped<IRecoveryManagerRepository, RecoveryManagerRepository>();
|
|
builder.Services.AddScoped<IAltOptionManagerRepository, AltOptionManagerRepository>();
|
|
builder.Services.AddScoped<IRoleManageRepository, RoleManageRepository>();
|
|
builder.Services.AddScoped<UserHelper>();
|
|
builder.Services.AddScoped<UserManagerService>();
|
|
builder.Services.AddScoped<RecoveryManagerService>();
|
|
builder.Services.AddScoped<AuthHex.Services.AltOptionManager.AltOptionManagerService>();
|
|
builder.Services.AddScoped<AuthHex.Services.RoleManager.RoleManagerService>();
|
|
builder.Services.AddHttpClient<ThirdPartyService>();
|
|
builder.Services.AddScoped<JwtTokenHelper>();
|
|
builder.Services.AddScoped<AuthHex.Services.FunctionHandler.UserManager>();
|
|
builder.Services.AddScoped<AuthHex.Services.FunctionHandler.RecoveryManager>();
|
|
builder.Services.AddScoped<AuthHex.Services.FunctionHandler.AltOptionManager>();
|
|
builder.Services.AddScoped<AuthHex.Services.FunctionHandler.RoleManager>();
|
|
|
|
var configuredOrigins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? Array.Empty<string>();
|
|
var allowedOrigins = configuredOrigins
|
|
.Where(origin => !string.IsNullOrWhiteSpace(origin) && origin != "*")
|
|
.ToList();
|
|
|
|
var frontendUrl = builder.Configuration["AppSettings:FrontendUrl"];
|
|
if (!string.IsNullOrWhiteSpace(frontendUrl) &&
|
|
!allowedOrigins.Contains(frontendUrl, StringComparer.OrdinalIgnoreCase))
|
|
{
|
|
allowedOrigins.Add(frontendUrl);
|
|
}
|
|
|
|
// Add CORS policy
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("AllowAll", policy =>
|
|
{
|
|
if (allowedOrigins.Count > 0)
|
|
{
|
|
policy.WithOrigins(allowedOrigins.ToArray())
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.AllowCredentials();
|
|
return;
|
|
}
|
|
|
|
policy.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader();
|
|
});
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseHttpsRedirection();
|
|
}
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseCors("AllowAll");
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run(); |