2020-10-22 14:59:36 +08:00
|
|
|
|
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
|
|
|
|
|
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
|
|
|
|
|
|
|
|
|
|
|
2021-10-18 00:42:29 +08:00
|
|
|
|
using System;
|
2021-04-21 18:02:01 +08:00
|
|
|
|
using System.Linq;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Autofac;
|
2021-01-14 23:35:54 +08:00
|
|
|
|
using Infrastructure;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using OpenAuth.App;
|
|
|
|
|
using OpenAuth.Repository;
|
2023-08-31 20:20:49 +08:00
|
|
|
|
using SqlSugar;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
|
|
|
|
|
namespace OpenAuth.IdentityServer
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
public IHostEnvironment Environment { get; }
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
public Startup(IConfiguration configuration, IHostEnvironment environment)
|
|
|
|
|
{
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
Environment = environment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddControllersWithViews();
|
|
|
|
|
|
|
|
|
|
var builder = services.AddIdentityServer()
|
|
|
|
|
.AddInMemoryIdentityResources(Config.GetIdentityResources())
|
|
|
|
|
.AddInMemoryApiResources(Config.GetApis())
|
|
|
|
|
.AddInMemoryClients(Config.GetClients(Environment.IsProduction()))
|
|
|
|
|
.AddProfileService<CustomProfileService>();
|
|
|
|
|
|
|
|
|
|
services.ConfigureNonBreakingSameSiteCookies();
|
|
|
|
|
|
|
|
|
|
services.AddCors();
|
|
|
|
|
// todo:如果正式 环境请用下面的方式限制随意访问跨域
|
|
|
|
|
// var origins = new []
|
|
|
|
|
// {
|
|
|
|
|
// "http://localhost:1803",
|
|
|
|
|
// "http://localhost:52789"
|
|
|
|
|
// };
|
|
|
|
|
// if (Environment.IsProduction())
|
|
|
|
|
// {
|
|
|
|
|
// origins = new []
|
|
|
|
|
// {
|
2021-07-22 19:33:55 +08:00
|
|
|
|
// "http://demo.openauth.net.cn:1803",
|
|
|
|
|
// "http://demo.openauth.net.cn:52789"
|
2020-10-22 14:59:36 +08:00
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
// services.AddCors(option=>option.AddPolicy("cors", policy =>
|
|
|
|
|
// policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(origins)));
|
|
|
|
|
|
|
|
|
|
//全部用测试环境,正式环境请参考https://www.cnblogs.com/guolianyu/p/9872661.html
|
|
|
|
|
//if (Environment.IsDevelopment())
|
|
|
|
|
//{
|
|
|
|
|
builder.AddDeveloperSigningCredential();
|
|
|
|
|
//}
|
|
|
|
|
//else
|
|
|
|
|
//{
|
|
|
|
|
// throw new Exception("need to configure key material");
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
services.AddAuthentication();
|
|
|
|
|
|
|
|
|
|
//映射配置文件
|
|
|
|
|
services.Configure<AppSetting>(Configuration.GetSection("AppSetting"));
|
2021-04-21 18:02:01 +08:00
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
//在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);
|
|
|
|
|
var dbType = dbtypes["OpenAuthDBContext"];
|
2023-08-31 20:20:49 +08:00
|
|
|
|
var connectionString = Configuration.GetConnectionString("OpenAuthDBContext");
|
2020-10-22 14:59:36 +08:00
|
|
|
|
if (dbType == Define.DBTYPE_SQLSERVER)
|
|
|
|
|
{
|
|
|
|
|
services.AddDbContext<OpenAuthDBContext>(options =>
|
2023-08-31 20:20:49 +08:00
|
|
|
|
options.UseSqlServer(connectionString));
|
2020-10-22 14:59:36 +08:00
|
|
|
|
}
|
2021-04-17 17:06:48 +08:00
|
|
|
|
else if(dbType == Define.DBTYPE_MYSQL) //mysql
|
2020-10-22 14:59:36 +08:00
|
|
|
|
{
|
|
|
|
|
services.AddDbContext<OpenAuthDBContext>(options =>
|
2023-08-31 20:20:49 +08:00
|
|
|
|
options.UseMySql(connectionString,new MySqlServerVersion(new Version(8, 0, 11))));
|
2020-10-22 14:59:36 +08:00
|
|
|
|
}
|
2021-04-17 17:06:48 +08:00
|
|
|
|
else //oracle
|
|
|
|
|
{
|
|
|
|
|
services.AddDbContext<OpenAuthDBContext>(options =>
|
2023-08-31 20:20:49 +08:00
|
|
|
|
options.UseOracle(connectionString, o=>o.UseOracleSQLCompatibility("11")));
|
2021-04-17 17:06:48 +08:00
|
|
|
|
}
|
2023-08-31 20:20:49 +08:00
|
|
|
|
|
2023-09-02 11:40:07 +08:00
|
|
|
|
var sqlsugarTypes = UtilMethods.EnumToDictionary<SqlSugar.DbType>();
|
|
|
|
|
var sugarDbtype = sqlsugarTypes.FirstOrDefault(it =>
|
|
|
|
|
dbtypes.ToDictionary(u => u.Key, v => v.Value.ToLower()).ContainsValue(it.Key));
|
2023-08-31 20:20:49 +08:00
|
|
|
|
|
2023-09-02 11:40:07 +08:00
|
|
|
|
services.AddScoped<ISqlSugarClient>(s =>
|
|
|
|
|
{
|
|
|
|
|
var sqlSugar = new SqlSugarClient(new ConnectionConfig()
|
2023-08-31 20:20:49 +08:00
|
|
|
|
{
|
2023-09-02 11:40:07 +08:00
|
|
|
|
DbType = sugarDbtype.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
|
|
|
|
});
|
2023-08-31 20:20:49 +08:00
|
|
|
|
return sqlSugar;
|
|
|
|
|
});
|
2020-10-22 14:59:36 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ConfigureContainer(ContainerBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
AutofacExt.InitAutofac(builder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Configure(IApplicationBuilder app, IHostEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.UseCookiePolicy();
|
|
|
|
|
|
|
|
|
|
//todo:测试可以允许任意跨域,正式环境要加权限
|
|
|
|
|
app.UseCors(builder => builder.AllowAnyOrigin()
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader());
|
|
|
|
|
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
|
|
app.UseIdentityServer();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
{
|
|
|
|
|
endpoints.MapDefaultControllerRoute();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|