First commit
This commit is contained in:
96
Homework/Program.cs
Normal file
96
Homework/Program.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// ======================
|
||||
// Lägg till Logger
|
||||
// ======================
|
||||
var fileLoggerOptions = new Logger.FileLoggerOptions
|
||||
{
|
||||
FileName = "app.log",
|
||||
LogDirectory = "Logs",
|
||||
MaxFileSizeBytes = 10 * 1024 * 1024, // 10 MB
|
||||
MaxBackupFiles = 5,
|
||||
MinimumLevel = Logger.LogLevel.Info
|
||||
};
|
||||
|
||||
builder.Services.AddSingleton<Logger.ILogger>(
|
||||
Logger.LoggerFactory.CreateCompositeLogger(Logger.LogLevel.Debug, fileLoggerOptions));
|
||||
|
||||
// ======================
|
||||
// Lägg till Controllers
|
||||
// ======================
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowFrontend",
|
||||
policy =>
|
||||
{
|
||||
policy.WithOrigins("http://localhost:5173")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
});
|
||||
});
|
||||
|
||||
// ======================
|
||||
// Läs JWT-inställningar
|
||||
// ======================
|
||||
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
|
||||
|
||||
var key = Encoding.UTF8.GetBytes(jwtSettings["Key"]);
|
||||
|
||||
// ======================
|
||||
// Lägg till 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 = jwtSettings["Issuer"],
|
||||
ValidAudience = jwtSettings["Audience"],
|
||||
IssuerSigningKey = new SymmetricSecurityKey(key)
|
||||
};
|
||||
});
|
||||
|
||||
// ======================
|
||||
// Registrera JwtService
|
||||
// ======================
|
||||
builder.Services.Configure<JwtSettings>(
|
||||
builder.Configuration.GetSection("JwtSettings"));
|
||||
|
||||
builder.Services.AddScoped<JwtService>();
|
||||
|
||||
// ======================
|
||||
// Swagger (för test)
|
||||
// ======================
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// ======================
|
||||
// Middleware
|
||||
// ======================
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
|
||||
app.UseCors("AllowFrontend");
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user