121 lines
4.2 KiB
C#
121 lines
4.2 KiB
C#
using System.Net;
|
|
using System.Text;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Newtonsoft.Json.Converters;
|
|
using NSwag;
|
|
using NSwag.Generation.Processors.Security;
|
|
using T120B165_ImgBoard.Data;
|
|
using T120B165_ImgBoard.Models;
|
|
using T120B165_ImgBoard.Services;
|
|
|
|
namespace T120B165_ImgBoard;
|
|
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
|
builder.Services.AddControllers().AddNewtonsoftJson(
|
|
options => options.SerializerSettings.Converters.Add(new StringEnumConverter()));
|
|
builder.Services.AddOpenApiDocument(cfg =>
|
|
{
|
|
cfg.OperationProcessors.Add(new OperationSecurityScopeProcessor("auth"));
|
|
cfg.DocumentProcessors.Add(new SecurityDefinitionAppender("auth", new OpenApiSecurityScheme
|
|
{
|
|
Type = OpenApiSecuritySchemeType.Http,
|
|
In = OpenApiSecurityApiKeyLocation.Header,
|
|
Scheme = "bearer",
|
|
BearerFormat = "jwt"
|
|
}));
|
|
});
|
|
|
|
|
|
builder.Services.AddIdentity<User, IdentityRole>()
|
|
.AddEntityFrameworkStores<ImgBoardContext>()
|
|
.AddDefaultTokenProviders();
|
|
|
|
|
|
builder.Services.AddScoped<ITagService, TagService>();
|
|
builder.Services.AddScoped<ITokenService, TokenService>();
|
|
builder.Services.AddScoped<IPostService, PostService>();
|
|
builder.Services.AddScoped<IUserService, UserService>();
|
|
builder.Services.AddScoped<ICommentService, CommentService>();
|
|
builder.Services.AddScoped<IFileService, FileService>();
|
|
|
|
// If we're in dev environment, use in-memory database
|
|
if (builder.Environment.IsDevelopment())
|
|
{
|
|
builder.Services.AddDbContext<ImgBoardContext>(options =>
|
|
options.UseInMemoryDatabase("T120B165_ImgBoard"));
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Running in production mode");
|
|
builder.Services.AddDbContext<ImgBoardContext>(options =>
|
|
options.UseMySql(
|
|
builder.Configuration.GetConnectionString("DbContext"),
|
|
new MySqlServerVersion(new Version(11, 8, 3))
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
var jwtSettings = builder.Configuration.GetSection("Jwt");
|
|
var key = Encoding.UTF8.GetBytes(jwtSettings["Key"]);
|
|
|
|
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)
|
|
};
|
|
});
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
//app.MapOpenApi();
|
|
app.UseOpenApi();
|
|
app.UseSwaggerUi();
|
|
}
|
|
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var services = scope.ServiceProvider;
|
|
|
|
var context = services.GetRequiredService<ImgBoardContext>();
|
|
context.Database.EnsureCreated();
|
|
DbInitializer.SeedAuth(services).GetAwaiter().GetResult();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
app.Run();
|
|
}
|
|
} |