2022-03-06 14:26:05 +08:00
|
|
|
|
using Infrastructure;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
|
using ZR.Admin.WebApi.Framework;
|
|
|
|
|
|
using Hei.Captcha;
|
|
|
|
|
|
using Infrastructure.Extensions;
|
|
|
|
|
|
using ZR.Admin.WebApi.Extensions;
|
|
|
|
|
|
using ZR.Admin.WebApi.Filters;
|
|
|
|
|
|
using ZR.Admin.WebApi.Middleware;
|
|
|
|
|
|
using ZR.Admin.WebApi.Hubs;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
|
2022-03-06 14:26:05 +08:00
|
|
|
|
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();
|
|
|
|
|
|
//ע<><D7A2>HttpContextAccessor
|
|
|
|
|
|
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>
|
|
|
|
|
|
builder.Services.AddCors(c =>
|
2021-08-23 16:57:25 +08:00
|
|
|
|
{
|
2022-03-06 14:26:05 +08:00
|
|
|
|
c.AddPolicy("Policy", policy =>
|
2021-08-23 16:57:25 +08:00
|
|
|
|
{
|
2022-03-06 14:26:05 +08:00
|
|
|
|
policy.WithOrigins(builder.Configuration["corsUrls"].Split(',', StringSplitOptions.RemoveEmptyEntries))
|
|
|
|
|
|
.AllowAnyHeader()//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
|
|
|
|
|
|
.AllowCredentials()//<2F><><EFBFBD><EFBFBD>cookie
|
|
|
|
|
|
.AllowAnyMethod();//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⷽ<EFBFBD><E2B7BD>
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//ע<><D7A2>SignalRʵʱͨѶ<CDA8><D1B6>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>json<6F><6E><EFBFBD><EFBFBD>
|
|
|
|
|
|
builder.Services.AddSignalR();
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>Error unprotecting the session cookie<69><65><EFBFBD><EFBFBD>
|
|
|
|
|
|
builder.Services.AddDataProtection()
|
|
|
|
|
|
.PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
|
|
|
|
|
|
//<2F><>ͨ<EFBFBD><CDA8>֤<EFBFBD><D6A4>
|
|
|
|
|
|
builder.Services.AddHeiCaptcha();
|
|
|
|
|
|
//builder.Services.AddSession();
|
|
|
|
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Model<65><6C>
|
|
|
|
|
|
builder.Services.Configure<OptionsSetting>(builder.Configuration);
|
|
|
|
|
|
|
|
|
|
|
|
//jwt <20><>֤
|
|
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
|
}).AddCookie()
|
|
|
|
|
|
.AddJwtBearer(o =>
|
|
|
|
|
|
{
|
|
|
|
|
|
o.TokenValidationParameters = JwtUtil.ValidParameters();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
InternalApp.InternalServices = builder.Services;
|
|
|
|
|
|
builder.Services.AddAppService();
|
|
|
|
|
|
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
builder.Services.AddTaskSchedulers();
|
|
|
|
|
|
//<2F><>ʼ<EFBFBD><CABC>db
|
|
|
|
|
|
DbExtension.AddDb(builder.Configuration);
|
|
|
|
|
|
|
|
|
|
|
|
//ע<><D7A2>REDIS <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
//RedisServer.Initalize();
|
|
|
|
|
|
});
|
|
|
|
|
|
builder.Services.AddMvc(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.Filters.Add(typeof(GlobalActionMonitor));//ȫ<><C8AB>ע<EFBFBD><D7A2>
|
|
|
|
|
|
})
|
|
|
|
|
|
.AddJsonOptions(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter());
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSwaggerConfig();
|
|
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
2022-05-19 18:34:52 +08:00
|
|
|
|
app.UseSwagger();
|
|
|
|
|
|
|
2022-03-06 14:26:05 +08:00
|
|
|
|
//ʹ<><CAB9><EFBFBD>Զ<EFBFBD><D4B6>ζ<EFBFBD>ȥbody<64><79><EFBFBD><EFBFBD>
|
|
|
|
|
|
app.Use((context, next) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Request.EnableBuffering();
|
|
|
|
|
|
if (context.Request.Query.TryGetValue("access_token", out var token))
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Request.Headers.Add("Authorization", $"Bearer {token}");
|
|
|
|
|
|
}
|
|
|
|
|
|
return next();
|
|
|
|
|
|
});
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>̬<EFBFBD>ļ<EFBFBD>/wwwrootĿ¼<C4BF>ļ<EFBFBD><C4BC><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>UseRoutingǰ<67><C7B0>
|
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>·<EFBFBD>ɷ<EFBFBD><C9B7><EFBFBD>
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
app.UseCors("Policy");//Ҫ<><D2AA><EFBFBD><EFBFBD>app.UseEndpointsǰ<73><C7B0>
|
|
|
|
|
|
//app.UseHttpsRedirection();
|
|
|
|
|
|
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
app.UseResponseCaching();
|
|
|
|
|
|
//<2F>ָ<EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
app.UseAddTaskSchedulers();
|
|
|
|
|
|
//ʹ<><CAB9>ȫ<EFBFBD><C8AB><EFBFBD>쳣<EFBFBD>м<EFBFBD><D0BC><EFBFBD>
|
|
|
|
|
|
app.UseMiddleware<GlobalExceptionMiddleware>();
|
|
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
|
{
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>socket<65><74><EFBFBD><EFBFBD>
|
|
|
|
|
|
endpoints.MapHub<MessageHub>("/msgHub");
|
|
|
|
|
|
|
|
|
|
|
|
endpoints.MapControllerRoute(
|
|
|
|
|
|
name: "default",
|
|
|
|
|
|
pattern: "{controller=Home}/{action=Index}/{id?}");
|
|
|
|
|
|
});
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
|
|
|
|
app.Run();
|