2021-08-23 16:57:25 +08:00
|
|
|
|
using Hei.Captcha;
|
|
|
|
|
|
using Infrastructure;
|
|
|
|
|
|
using Infrastructure.Extensions;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Razor;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
2021-10-11 18:05:01 +08:00
|
|
|
|
using SqlSugar.IOC;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using System.IO;
|
2021-10-11 18:05:01 +08:00
|
|
|
|
using System.Linq;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using ZR.Admin.WebApi.Extensions;
|
|
|
|
|
|
using ZR.Admin.WebApi.Filters;
|
2021-11-23 09:55:37 +08:00
|
|
|
|
using ZR.Admin.WebApi.Framework;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using ZR.Admin.WebApi.Middleware;
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZR.Admin.WebApi
|
|
|
|
|
|
{
|
|
|
|
|
|
public class Startup
|
|
|
|
|
|
{
|
2021-10-29 14:05:29 +08:00
|
|
|
|
public Startup(IConfiguration configuration, IWebHostEnvironment hostEnvironment)
|
2021-08-23 16:57:25 +08:00
|
|
|
|
{
|
|
|
|
|
|
Configuration = configuration;
|
2021-10-29 14:05:29 +08:00
|
|
|
|
CurrentEnvironment = hostEnvironment;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-29 14:05:29 +08:00
|
|
|
|
private IWebHostEnvironment CurrentEnvironment { get; }
|
2021-08-23 16:57:25 +08:00
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
|
{
|
|
|
|
|
|
string corsUrls = Configuration["sysConfig:cors"];
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>
|
|
|
|
|
|
services.AddCors(c =>
|
|
|
|
|
|
{
|
|
|
|
|
|
c.AddPolicy("Policy", policy =>
|
|
|
|
|
|
{
|
|
|
|
|
|
policy.WithOrigins(corsUrls.Split(',', System.StringSplitOptions.RemoveEmptyEntries))
|
|
|
|
|
|
.AllowAnyHeader()//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
|
|
|
|
|
|
.AllowCredentials()//<2F><><EFBFBD><EFBFBD>cookie
|
|
|
|
|
|
.AllowAnyMethod();//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⷽ<EFBFBD><E2B7BD>
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>Error unprotecting the session cookie<69><65><EFBFBD><EFBFBD>
|
|
|
|
|
|
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
|
|
|
|
|
|
//<2F><>ͨ<EFBFBD><CDA8>֤<EFBFBD><D6A4>
|
|
|
|
|
|
services.AddHeiCaptcha();
|
|
|
|
|
|
services.AddSession();
|
|
|
|
|
|
services.AddHttpContextAccessor();
|
|
|
|
|
|
|
|
|
|
|
|
//Cookie <20><>֤
|
|
|
|
|
|
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Model<65><6C>
|
|
|
|
|
|
services.Configure<OptionsSetting>(Configuration);
|
|
|
|
|
|
|
|
|
|
|
|
InjectRepositories(services);
|
|
|
|
|
|
|
|
|
|
|
|
services.AddMvc(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.Filters.Add(typeof(GlobalActionMonitor));//ȫ<><C8AB>ע<EFBFBD><D7A2><EFBFBD>쳣
|
|
|
|
|
|
})
|
|
|
|
|
|
.AddMvcLocalization()
|
2021-11-23 09:55:37 +08:00
|
|
|
|
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
|
|
|
|
|
|
.AddJsonOptions(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter());
|
|
|
|
|
|
});
|
2021-08-23 16:57:25 +08:00
|
|
|
|
|
|
|
|
|
|
services.AddSwaggerGen(c =>
|
|
|
|
|
|
{
|
|
|
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ZrAdmin", Version = "v1" });
|
2021-10-29 14:05:29 +08:00
|
|
|
|
if (CurrentEnvironment.IsDevelopment())
|
|
|
|
|
|
{
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ע<EFBFBD><D7A2>
|
|
|
|
|
|
c.IncludeXmlComments("ZRAdmin.xml", true);
|
|
|
|
|
|
}
|
2021-08-23 16:57:25 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
|
{
|
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
|
}
|
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
|
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ZrAdmin v1"));
|
|
|
|
|
|
|
|
|
|
|
|
//ʹ<><CAB9><EFBFBD>Զ<EFBFBD><D4B6>ζ<EFBFBD>ȥbody<64><79><EFBFBD><EFBFBD>
|
|
|
|
|
|
app.Use((context, next) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Request.EnableBuffering();
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
app.UseCors("Policy");//Ҫ<><D2AA><EFBFBD><EFBFBD>app.UseEndpointsǰ<73><C7B0>
|
|
|
|
|
|
|
|
|
|
|
|
//app.UseAuthentication<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Authentication<6F>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ǰHttp<74><70><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Cookie<69><65>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>HttpContext.User<65><72><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>app.UseAuthentication<6F><6E><EFBFBD><EFBFBD>֮<EFBFBD><D6AE>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD>HttpContext.User<65>ж<EFBFBD>ȡ<EFBFBD><C8A1>ֵ<EFBFBD><D6B5>
|
|
|
|
|
|
//<2F><>Ҳ<EFBFBD><D2B2>Ϊʲô<CAB2><C3B4><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF>app.UseAuthentication<6F><6E><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>app.UseMvc<76><63><EFBFBD><EFBFBD>ǰ<EFBFBD>棬<EFBFBD><E6A3AC>Ϊֻ<CEAA><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ASP.NET Core<72><65>MVC<56>м<EFBFBD><D0BC><EFBFBD><EFBFBD>в<EFBFBD><D0B2>ܶ<EFBFBD>ȡ<EFBFBD><C8A1>HttpContext.User<65><72>ֵ<EFBFBD><D6B5>
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
app.UseSession();
|
|
|
|
|
|
app.UseResponseCaching();
|
|
|
|
|
|
|
|
|
|
|
|
// <20>ָ<EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
app.UseAddTaskSchedulers();
|
|
|
|
|
|
|
|
|
|
|
|
app.UseMiddleware<GlobalExceptionMiddleware>();
|
|
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
|
{
|
|
|
|
|
|
endpoints.MapControllerRoute(
|
|
|
|
|
|
name: "default",
|
|
|
|
|
|
pattern: "{controller=Home}/{action=Index}/{id?}");
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// ע<><D7A2>Services<65><73><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="services"></param>
|
2021-10-11 18:05:01 +08:00
|
|
|
|
private void InjectRepositories(IServiceCollection services)
|
2021-08-23 16:57:25 +08:00
|
|
|
|
{
|
|
|
|
|
|
services.AddAppService();
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ƻ<EFBFBD><C6BB><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
services.AddTaskSchedulers();
|
2021-10-11 18:05:01 +08:00
|
|
|
|
|
|
|
|
|
|
string connStr = Configuration.GetConnectionString(OptionsSetting.ConnAdmin);
|
|
|
|
|
|
string connStrBus = Configuration.GetConnectionString(OptionsSetting.ConnBus);
|
|
|
|
|
|
string dbKey = Configuration[OptionsSetting.DbKey];
|
|
|
|
|
|
int dbType = Convert.ToInt32(Configuration[OptionsSetting.ConnDbType]);
|
|
|
|
|
|
int dbType_bus = Convert.ToInt32(Configuration[OptionsSetting.ConnBusDbType]);
|
|
|
|
|
|
|
2021-10-29 13:38:41 +08:00
|
|
|
|
SugarIocServices.AddSqlSugar(new List<IocConfig>() {
|
|
|
|
|
|
new IocConfig() {
|
2021-11-27 09:43:04 +08:00
|
|
|
|
ConfigId = "0",
|
2021-10-11 18:05:01 +08:00
|
|
|
|
ConnectionString = connStr,
|
|
|
|
|
|
DbType = (IocDbType)dbType,
|
|
|
|
|
|
IsAutoCloseConnection = true//<2F>Զ<EFBFBD><D4B6>ͷ<EFBFBD>
|
2021-10-29 13:38:41 +08:00
|
|
|
|
}, new IocConfig() {
|
2021-11-27 09:43:04 +08:00
|
|
|
|
ConfigId = "1",
|
2021-10-11 18:05:01 +08:00
|
|
|
|
ConnectionString = connStrBus,
|
|
|
|
|
|
DbType = (IocDbType)dbType_bus,
|
|
|
|
|
|
IsAutoCloseConnection = true//<2F>Զ<EFBFBD><D4B6>ͷ<EFBFBD>
|
2021-10-29 13:38:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2021-10-11 18:05:01 +08:00
|
|
|
|
|
|
|
|
|
|
//<2F><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡSQL
|
2021-11-27 09:43:04 +08:00
|
|
|
|
DbScoped.SugarScope.GetConnection("0").Aop.OnLogExecuting = (sql, pars) =>
|
2021-10-11 18:05:01 +08:00
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("<22><>SQL<51><4C><EFBFBD>䡿" + sql.ToLower() + "\r\n"
|
|
|
|
|
|
+ DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
|
|
|
|
|
};
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>־
|
2021-11-27 09:43:04 +08:00
|
|
|
|
DbScoped.SugarScope.GetConnection("0").Aop.OnError = (e) =>
|
2021-10-11 18:05:01 +08:00
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"[ִ<><D6B4>Sql<71><6C><EFBFBD><EFBFBD>]{e.Message}<7D><>SQL={e.Sql}");
|
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
};
|
2021-10-29 13:23:22 +08:00
|
|
|
|
|
|
|
|
|
|
//<2F><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡSQL
|
|
|
|
|
|
DbScoped.SugarScope.GetConnection(1).Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("<22><>SQL<51><4C><EFBFBD><EFBFBD>Bus<75><73>" + sql.ToLower() + "\r\n"
|
|
|
|
|
|
+ DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
|
|
|
|
|
};
|
2021-10-11 18:05:01 +08:00
|
|
|
|
//Bus Db<44><62><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
|
|
|
|
|
DbScoped.SugarScope.GetConnection(1).Aop.OnError = (e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"[ִ<><D6B4>Sql<71><6C><EFBFBD><EFBFBD>Bus]{e.Message}<7D><>SQL={e.Sql}");
|
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
};
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|