skantravels/API/Program.cs

123 lines
4.7 KiB
C#
Raw Normal View History

using API.Application.Users.Commands;
using API.Application.Users.Queries;
using API.Persistence.Repositories;
2024-08-14 09:28:12 +01:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
2024-08-12 09:58:27 +01:00
using Microsoft.EntityFrameworkCore;
2024-08-14 09:28:12 +01:00
using Microsoft.IdentityModel.Tokens;
2024-08-15 11:26:02 +01:00
using Microsoft.OpenApi.Models;
2024-08-14 09:28:12 +01:00
using System.Text;
2024-08-12 09:58:27 +01:00
2024-08-05 08:00:16 +01:00
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<QueryAllUsers>();
builder.Services.AddScoped<QueryUserById>();
builder.Services.AddScoped<CreateUser>();
builder.Services.AddScoped<UpdateUser>();
2024-08-16 11:53:39 +01:00
builder.Services.AddScoped<UpdateUserPassword>();
builder.Services.AddScoped<DeleteUser>();
2024-08-14 09:28:12 +01:00
builder.Services.AddScoped<LoginUser>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
2024-08-14 09:28:12 +01:00
2024-08-12 09:58:27 +01:00
IConfiguration Configuration = builder.Configuration;
2024-08-14 09:28:12 +01:00
// 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
};
});
2024-08-15 11:26:02 +01:00
// Swagger does not by default allow to use Bearer tokens
// The method AddSwaggerGen with the following options grants access to address a Bearer token -
// Simply by clicking the Lock icon and pasting the Bearer Token
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Your API", Version = "v1" });
// Configure Swagger to use Bearer token authentication
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme",
Type = SecuritySchemeType.Http,
Scheme = "bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] { }
}
});
});
2024-08-13 18:08:30 +01:00
var connectionString = Configuration.GetConnectionString("DefaultConnection") ?? Environment.GetEnvironmentVariable("DEFAULT_CONNECTION");
2024-08-12 09:58:27 +01:00
builder.Services.AddDbContext<AppDBContext>(options => options.UseSqlite(connectionString));
2024-08-13 18:08:30 +01:00
Console.WriteLine("Connecting to database with connection string: " + connectionString);
2024-08-05 08:00:16 +01:00
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
app.UseCors(MyAllowSpecificOrigins);
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}