116 lines
No EOL
3.9 KiB
C#
116 lines
No EOL
3.9 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using SurveyBackend.Contexts;
|
|
using SurveyBackend.Core.Contexts;
|
|
using SurveyBackend.Core.Repositories;
|
|
using SurveyBackend.Core.Services;
|
|
using SurveyBackend.Infrastructure.Data;
|
|
using SurveyBackend.Infrastructure.Repositories;
|
|
using SurveyBackend.Middlewares;
|
|
using SurveyBackend.Services;
|
|
using SurveyBackend.Services.Services;
|
|
using SurveyLib.Core.Repositories;
|
|
using SurveyLib.Core.Services;
|
|
using SurveyLib.Infrastructure.EFCore.Data;
|
|
using SurveyLib.Infrastructure.EFCore.Repositories;
|
|
|
|
namespace SurveyBackend;
|
|
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
AuthOptions.MakeOptions(builder.Configuration, Environment.GetEnvironmentVariable("JWT_SECRET_KEY"));
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
|
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
builder.Services.AddScoped<SurveyDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());
|
|
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
builder.Services.AddScoped<IUserContext, UserContext>();
|
|
|
|
builder.Services.AddScoped<IUserRepository, UserRepository>();
|
|
builder.Services.AddScoped<IUserService, UserService>();
|
|
|
|
builder.Services.AddScoped<IPasswordHasher, Sha256PasswordHasher>();
|
|
|
|
builder.Services.AddScoped<IAuthorizationService, AuthorizationService>();
|
|
|
|
builder.Services.AddScoped<ISurveyRepository, SurveyRepository>();
|
|
builder.Services.AddScoped<IQuestionRepository, QuestionRepository>();
|
|
|
|
builder.Services.AddScoped<ISurveyService, SurveyService>();
|
|
builder.Services.AddScoped<IQuestionService, QuestionService>();
|
|
|
|
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = AuthOptions.Issuer,
|
|
ValidAudience = AuthOptions.Audience,
|
|
IssuerSigningKey = AuthOptions.SymmetricSecurityKey
|
|
};
|
|
});
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
Scheme = "bearer",
|
|
BearerFormat = "JWT",
|
|
In = ParameterLocation.Header,
|
|
Description = "JWT Authorization header using the Bearer scheme.",
|
|
Name = "Authorization",
|
|
Type = SecuritySchemeType.ApiKey
|
|
});
|
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = JwtBearerDefaults.AuthenticationScheme
|
|
}
|
|
},
|
|
Array.Empty<string>()
|
|
}
|
|
});
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseMiddleware<ExceptionsMiddleware>();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|
|
}
|
|
} |