using API.Application.Users.Commands; using API.Application.Users.Queries; using API.Persistence.Repositories; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using System.Text; namespace API { public class Program { public static void Main(string[] args) { var MyAllowSpecificOrigins = "_myAllowSpecificOrigins"; var builder = WebApplication.CreateBuilder(args); builder.Services.AddCors(options => { options.AddPolicy( name: MyAllowSpecificOrigins, policy => { policy.WithOrigins("*").AllowAnyMethod().AllowAnyHeader(); } ); }); // 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(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); IConfiguration Configuration = builder.Configuration; // Configure JWT Authentication builder.Services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(x => { x.TokenValidationParameters = new TokenValidationParameters { ValidIssuer = Configuration["JwtSettings:Issuer"], ValidAudience = Configuration["JwtSettings:Audience"], IssuerSigningKey = new SymmetricSecurityKey ( Encoding.UTF8.GetBytes(Configuration["JwtSettings:Key"]) ), ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true }; }); var connectionString = Configuration.GetConnectionString("DefaultConnection") ?? Environment.GetEnvironmentVariable("DEFAULT_CONNECTION"); builder.Services.AddDbContext(options => options.UseSqlite(connectionString)); Console.WriteLine("Connecting to database with connection string: " + connectionString); var app = builder.Build(); // Configure the HTTP request pipeline. app.UseSwagger(); app.UseSwaggerUI(); app.UseHttpsRedirection(); app.UseCors(MyAllowSpecificOrigins); app.UseAuthorization(); app.MapControllers(); app.Run(); } } }