代码拉取完成,页面将自动刷新
using JurassicCentury;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.IdentityModel.Tokens;
using Models;
using StackExchange.Redis;
using System.Text;
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();
string[] AllowAccess = builder.Configuration.GetSection("AllowAccess").Get<string[]>();
//设置可直接访问后端的前端端口
//new string[] { "http://127.0.0.1:5500","http://localhost:8080" }
builder.Services.AddCors(b => b.AddDefaultPolicy(b => b.WithOrigins(AllowAccess).AllowAnyMethod().AllowAnyHeader().AllowCredentials()));
//注入MyDbContext服务
builder.Services.TryAddTransient<_MyDbContext>();
//注入web环境服务
builder.Services.TryAddTransient<IWebHostEnvironment>();
//注入redis服务
builder.Services.AddTransient(r =>
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(builder.Configuration.GetConnectionString("Redis"));
return redis;
});
//允许嵌套Json传出
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
});
//JWT
builder.Services.Configure<JWTSettings>(builder.Configuration.GetSection("JWT"));
//最大上传文件
builder.Services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = int.MaxValue;
});
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(j =>
{
var jwtOpt = builder.Configuration.GetSection("JWT").Get<JWTSettings>();
byte[] keyBytes = Encoding.UTF8.GetBytes(jwtOpt.SecKey);
var SecKey = new SymmetricSecurityKey(keyBytes);
j.TokenValidationParameters = new()
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = SecKey
};
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//开启Cors服务
app.UseCors();
app.UseHttpsRedirection();
//开启身份验证
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
var provider = new FileExtensionContentTypeProvider();
// 添加新的映射
provider.Mappings[".gltf"] = "model/gltf+json";
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = provider
});
//配置静态资源文件夹
app.UseStaticFiles();
app.Run();
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。