2024-11-03 11:21:47 +08:00
|
|
|
|
using System.Runtime.Serialization.Formatters;
|
|
|
|
|
using System.Text;
|
2024-10-30 17:52:22 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-11-03 21:58:05 +08:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2024-10-30 17:52:22 +08:00
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using SiteManagementSystem_SoftwareEngineering_.Extension;
|
2024-11-03 11:21:47 +08:00
|
|
|
|
using SiteManagementSystem_SoftwareEngineering_.Interface;
|
2024-10-30 17:52:22 +08:00
|
|
|
|
using SiteManagementSystem_SoftwareEngineering_.Service;
|
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
|
// 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.AddDbContext<SQLService>(options =>
|
|
|
|
|
options.UseMySql(
|
|
|
|
|
builder.Configuration.GetConnectionString("SQL"),
|
2024-11-04 22:26:45 +08:00
|
|
|
|
MySqlServerVersion.AutoDetect(builder.Configuration.GetConnectionString("SQL"))
|
|
|
|
|
//MariaDbServerVersion.AutoDetect(builder.Configuration.GetConnectionString("SQL"))
|
2024-10-30 17:52:22 +08:00
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
builder
|
|
|
|
|
.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
.AddJwtBearer(options =>
|
|
|
|
|
{
|
|
|
|
|
var jwtConfig = builder.Configuration.GetSection("Jwt");
|
|
|
|
|
var key = Encoding.UTF8.GetBytes(jwtConfig.GetValue<string>("SecurityKey")!);
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
ValidateIssuer = true,
|
|
|
|
|
ValidateAudience = true,
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
|
ValidIssuer = jwtConfig.GetValue<string>("Issuer"),
|
|
|
|
|
ValidAudience = jwtConfig.GetValue<string>("Audience"),
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(key)
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
builder
|
|
|
|
|
.Services.AddTokenFactory(options =>
|
|
|
|
|
{
|
|
|
|
|
var config = builder.Configuration.GetSection("Jwt");
|
|
|
|
|
options.Issuer = config.GetValue<string>("Issuer")!;
|
|
|
|
|
options.Audience = config.GetValue<string>("Audience")!;
|
|
|
|
|
options.SigningKey = config.GetValue<string>("SecurityKey")!;
|
|
|
|
|
options.AccessTokenExpire = config.GetValue<int>("AccessTokenExpire");
|
|
|
|
|
options.RefreshTokenExpire = config.GetValue<int>("RefreshTokenExpire");
|
|
|
|
|
options.RefreshTokenBefore = config.GetValue<int>("RefreshTokenBefore");
|
|
|
|
|
})
|
|
|
|
|
.AddUserManager(options =>
|
|
|
|
|
options.HashSalt = builder.Configuration.GetValue<string>("SecretSalt")!
|
2024-11-03 21:58:05 +08:00
|
|
|
|
).AddScoped<IFieldService, FieldService>()
|
|
|
|
|
.AddRedisUtils(options =>
|
|
|
|
|
{
|
|
|
|
|
options.RedisConnectionString = builder.Configuration.GetConnectionString("Redis")!;
|
|
|
|
|
options.RedisDB = int.Parse(builder.Configuration["RedisDB"] ?? "0");
|
|
|
|
|
}
|
|
|
|
|
).AddSingleton(typeof(ICacheService<>), typeof(RedisService<>));
|
2024-10-30 17:52:22 +08:00
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
|
|
app.Run();
|