|
|
|
|
using BuaaLocationServer.Middlewares.Db;
|
|
|
|
|
using BuaaLocationServer.Middlewares.Jwts;
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
|
// Add services to the container.
|
|
|
|
|
builder.Services.AddDbContext<BuaaDbContext>((provider, options) =>
|
|
|
|
|
{
|
|
|
|
|
if (!builder.Environment.IsProduction()) options.EnableSensitiveDataLogging();
|
|
|
|
|
options.UseSqlite(builder.Configuration.GetConnectionString("SQLite"),
|
|
|
|
|
opts => opts.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
|
|
|
|
|
});
|
|
|
|
|
builder.Services.Configure<JwtOptions>(builder.Configuration.GetSection("JwtSettings"));
|
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
|
builder.Services.AddAuthentication("Bearer").AddJwtBearer(o => {
|
|
|
|
|
o.RequireHttpsMetadata = false;
|
|
|
|
|
var secret = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JwtSettings:Secret"] ?? string.Empty));
|
|
|
|
|
o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
NameClaimType = JwtClaimTypes.Name,
|
|
|
|
|
RoleClaimType = JwtClaimTypes.Role,
|
|
|
|
|
|
|
|
|
|
ValidateIssuer = true,
|
|
|
|
|
ValidIssuer = builder.Configuration["JwtSettings:Issuer"],
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
|
IssuerSigningKey = secret,
|
|
|
|
|
ValidateAudience = true,
|
|
|
|
|
ValidAudience = builder.Configuration["JwtSettings:Audience"],
|
|
|
|
|
RequireExpirationTime = true,
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ClockSkew = TimeSpan.FromSeconds(30)
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSingleton<IJwtService, JwtService>();
|
|
|
|
|
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
|
builder.Services.AddSwaggerGen(options =>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Description = "ʹ<EFBFBD><EFBFBD> JWT Bearer ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>: \"Bearer {token}\"",
|
|
|
|
|
Name = "Authorization", //Jwt default param name
|
|
|
|
|
In = ParameterLocation.Header, //Jwt store address
|
|
|
|
|
Type = SecuritySchemeType.ApiKey, //Security scheme type
|
|
|
|
|
Scheme = "Bearer",
|
|
|
|
|
BearerFormat = "JWT"
|
|
|
|
|
});
|
|
|
|
|
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Reference = new OpenApiReference
|
|
|
|
|
{
|
|
|
|
|
Type = ReferenceType.SecurityScheme,
|
|
|
|
|
Id = "Bearer"
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Array.Empty<string>()
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
builder.Services.AddMemoryCache();
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI(o => o.EnablePersistAuthorization());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//app.UseHttpsRedirection();
|
|
|
|
|
app.SetupDatabase();
|
|
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
|
|
app.Run();
|