2020-10-22 14:59:36 +08:00
|
|
|
|
using System.IO;
|
2021-04-21 18:02:01 +08:00
|
|
|
|
using System.Linq;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Autofac;
|
2021-01-04 22:17:49 +08:00
|
|
|
|
using Infrastructure;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Infrastructure.Extensions.AutofacManager;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2021-01-04 22:17:49 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using OpenAuth.App;
|
|
|
|
|
using OpenAuth.App.HostedService;
|
|
|
|
|
using OpenAuth.Mvc.Models;
|
|
|
|
|
using OpenAuth.Repository;
|
2023-08-28 12:33:45 +08:00
|
|
|
|
using SqlSugar;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
|
|
|
|
|
namespace OpenAuth.Mvc
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
}
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
2021-05-09 00:02:28 +08:00
|
|
|
|
//在startup中需要强制创建log4net
|
|
|
|
|
var loggerFactory = LoggerFactory.Create(builder =>
|
2021-01-04 22:17:49 +08:00
|
|
|
|
{
|
2021-05-09 00:02:28 +08:00
|
|
|
|
builder.AddLog4Net();
|
2021-01-04 22:17:49 +08:00
|
|
|
|
});
|
2021-05-09 00:02:28 +08:00
|
|
|
|
ILogger logger = loggerFactory.CreateLogger<Startup>();
|
2020-10-22 14:59:36 +08:00
|
|
|
|
var identityServer = ((ConfigurationSection)Configuration.GetSection("AppSetting:IdentityServerUrl")).Value;
|
|
|
|
|
if (!string.IsNullOrEmpty(identityServer))
|
|
|
|
|
{
|
|
|
|
|
System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
|
|
|
|
|
|
|
|
|
services.AddAuthentication(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DefaultScheme = "Cookies";
|
|
|
|
|
options.DefaultChallengeScheme = "oidc";
|
|
|
|
|
})
|
|
|
|
|
.AddCookie("Cookies")
|
|
|
|
|
.AddOpenIdConnect("oidc", options =>
|
|
|
|
|
{
|
|
|
|
|
options.Authority = identityServer;
|
|
|
|
|
options.RequireHttpsMetadata = false;
|
|
|
|
|
|
|
|
|
|
options.ClientId = "OpenAuth.Mvc";
|
|
|
|
|
options.SaveTokens = true;
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
NameClaimType = "name",
|
|
|
|
|
RoleClaimType = "role",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.NonceCookie.SameSite = SameSiteMode.Unspecified;
|
|
|
|
|
options.CorrelationCookie.SameSite = SameSiteMode.Unspecified;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
services.Configure<CookiePolicyOptions>(options =>
|
|
|
|
|
{
|
|
|
|
|
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
|
|
|
|
|
//关闭GDPR规范
|
|
|
|
|
options.CheckConsentNeeded = context => false;
|
|
|
|
|
options.MinimumSameSitePolicy = SameSiteMode.None;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
services.AddControllersWithViews(option =>
|
|
|
|
|
{
|
|
|
|
|
option.Filters.Add< OpenAuthFilter>();
|
|
|
|
|
option.ModelBinderProviders.Insert(0, new JsonBinderProvider());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddMemoryCache();
|
|
|
|
|
services.AddOptions();
|
|
|
|
|
|
|
|
|
|
services.AddRouting(options => options.LowercaseUrls = false);
|
|
|
|
|
|
|
|
|
|
//映射配置文件
|
|
|
|
|
services.Configure<AppSetting>(Configuration.GetSection("AppSetting"));
|
|
|
|
|
|
|
|
|
|
//在startup里面只能通过这种方式获取到appsettings里面的值,不能用IOptions😰
|
2021-04-21 18:02:01 +08:00
|
|
|
|
var dbtypes = ((ConfigurationSection)Configuration.GetSection("AppSetting:DbTypes")).GetChildren()
|
|
|
|
|
.ToDictionary(x => x.Key, x => x.Value);
|
2021-01-04 22:17:49 +08:00
|
|
|
|
var connectionString = Configuration.GetConnectionString("OpenAuthDBContext");
|
2021-04-21 18:02:01 +08:00
|
|
|
|
logger.LogInformation($"系统配置的数据库类型:{JsonHelper.Instance.Serialize(dbtypes)},连接字符串:{connectionString}");
|
2021-01-04 22:17:49 +08:00
|
|
|
|
|
2021-01-27 13:42:38 +08:00
|
|
|
|
services.AddDbContext<OpenAuthDBContext>();
|
2020-10-22 14:59:36 +08:00
|
|
|
|
|
|
|
|
|
services.AddHttpClient();
|
|
|
|
|
|
|
|
|
|
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Configuration["DataProtection"]));
|
|
|
|
|
|
2023-09-02 11:40:07 +08:00
|
|
|
|
var sqlsugarTypes = UtilMethods.EnumToDictionary<SqlSugar.DbType>();
|
|
|
|
|
var dbType = sqlsugarTypes.FirstOrDefault(it =>
|
|
|
|
|
dbtypes.ToDictionary(u => u.Key, v => v.Value.ToLower()).ContainsValue(it.Key));
|
2023-08-28 12:33:45 +08:00
|
|
|
|
|
2023-09-02 11:40:07 +08:00
|
|
|
|
services.AddScoped<ISqlSugarClient>(s =>
|
|
|
|
|
{
|
|
|
|
|
var sqlSugar = new SqlSugarClient(new ConnectionConfig()
|
2023-08-28 12:33:45 +08:00
|
|
|
|
{
|
2023-09-02 11:40:07 +08:00
|
|
|
|
DbType = dbType.Value,
|
|
|
|
|
ConnectionString = connectionString,
|
|
|
|
|
IsAutoCloseConnection = true,
|
2024-03-08 16:57:05 +08:00
|
|
|
|
MoreSettings=new ConnMoreSettings() {
|
|
|
|
|
PgSqlIsAutoToLower = false,//增删查改支持驼峰表
|
|
|
|
|
PgSqlIsAutoToLowerCodeFirst = false, // 建表建驼峰表。5.1.3.30
|
|
|
|
|
IsAutoToUpper=false //禁用自动转成大写表
|
|
|
|
|
}
|
2023-09-02 11:40:07 +08:00
|
|
|
|
}, db => { db.Aop.OnLogExecuting = (sql, pars) => { logger.LogInformation(sql); }; });
|
2023-08-28 12:33:45 +08:00
|
|
|
|
return sqlSugar;
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
//设置定时启动的任务
|
|
|
|
|
services.AddHostedService<QuartzService>();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ConfigureContainer(ContainerBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
AutofacExt.InitAutofac(builder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
2021-02-20 22:06:02 +08:00
|
|
|
|
public void Configure(IApplicationBuilder app, IHostEnvironment env, ILoggerFactory loggerFactory)
|
2020-10-22 14:59:36 +08:00
|
|
|
|
{
|
2021-02-20 22:06:02 +08:00
|
|
|
|
loggerFactory.AddLog4Net();
|
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
app.UseExceptionHandler("/Home/Error");
|
|
|
|
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
|
|
|
app.UseHsts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
|
|
|
|
//配置ServiceProvider
|
|
|
|
|
AutofacContainerModule.ConfigServiceProvider(app.ApplicationServices);
|
|
|
|
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
{
|
|
|
|
|
endpoints.MapControllerRoute(
|
|
|
|
|
name: "default",
|
|
|
|
|
pattern: "{controller=Home}/{action=Index}/{id?}");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|