4 Star 0 Fork 0

康康在IT界打怪/侏罗世纪

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Program.cs 2.73 KB
一键复制 编辑 原始数据 按行查看 历史
邹光义 提交于 2024-08-05 11:49 . 配置文件中配置跨域路由
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();
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kangkang-in-itjie/JonJack.git
git@gitee.com:kangkang-in-itjie/JonJack.git
kangkang-in-itjie
JonJack
侏罗世纪
master

搜索帮助