82 lines
2.4 KiB
C#
82 lines
2.4 KiB
C#
using System.Text;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using Newtonsoft.Json;
|
|
using Server.Data;
|
|
using Server.Models;
|
|
using Server.Services;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
|
|
builder.Services.AddControllers().AddNewtonsoftJson(o => {
|
|
o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|
});
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(o => {
|
|
o.TokenValidationParameters = new TokenValidationParameters {
|
|
ValidateActor = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
|
ValidAudience = builder.Configuration["Jwt:Audience"],
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:key"]))
|
|
};
|
|
});
|
|
builder.Services.AddAuthorization();
|
|
|
|
builder.Services.AddSwaggerGen(o => {
|
|
o.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme {
|
|
Scheme = "Bearer",
|
|
BearerFormat = "JWT",
|
|
In = ParameterLocation.Header,
|
|
Name = "Authorization",
|
|
Description = "Bearer Authentication with JWT Token",
|
|
Type = SecuritySchemeType.Http
|
|
});
|
|
o.AddSecurityRequirement(new OpenApiSecurityRequirement {
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Id = "Bearer",
|
|
Type = ReferenceType.SecurityScheme
|
|
}
|
|
},
|
|
new List<string>()
|
|
}
|
|
});
|
|
});
|
|
|
|
builder.Services.AddDbContext<ServerDbContext>(o =>
|
|
o.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
builder.Services.AddScoped<ScoreboardService>();
|
|
builder.Services.AddScoped<AuthenticationService>();
|
|
|
|
var jwt = new Jwt();
|
|
builder.Configuration.Bind("Jwt", jwt);
|
|
builder.Services.AddSingleton(jwt);
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.MapControllers();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.Run(); |