// 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. using System; using System.Linq; using Autofac; using Infrastructure; 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; using SqlSugar; 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(); services.ConfigureNonBreakingSameSiteCookies(); services.AddCors(); // todo:如果正式 环境请用下面的方式限制随意访问跨域 // var origins = new [] // { // "http://localhost:1803", // "http://localhost:52789" // }; // if (Environment.IsProduction()) // { // origins = new [] // { // "http://demo.openauth.net.cn:1803", // "http://demo.openauth.net.cn:52789" // }; // } // 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(Configuration.GetSection("AppSetting")); //在startup里面只能通过这种方式获取到appsettings里面的值,不能用IOptions😰 var dbtypes = ((ConfigurationSection)Configuration.GetSection("AppSetting:DbTypes")).GetChildren() .ToDictionary(x => x.Key, x => x.Value); var dbType = dbtypes["OpenAuthDBContext"]; var connectionString = Configuration.GetConnectionString("OpenAuthDBContext"); if (dbType == Define.DBTYPE_SQLSERVER) { services.AddDbContext(options => options.UseSqlServer(connectionString)); } else if(dbType == Define.DBTYPE_MYSQL) //mysql { services.AddDbContext(options => options.UseMySql(connectionString,new MySqlServerVersion(new Version(8, 0, 11)))); } else //oracle { services.AddDbContext(options => options.UseOracle(connectionString, o=>o.UseOracleSQLCompatibility("11"))); } var sqlsugarTypes = UtilMethods.EnumToDictionary(); var sugarDbtype = sqlsugarTypes.FirstOrDefault(it => dbtypes.ToDictionary(u => u.Key, v => v.Value.ToLower()).ContainsValue(it.Key)); services.AddScoped(s => { var sqlSugar = new SqlSugarClient(new ConnectionConfig() { DbType = sugarDbtype.Value, ConnectionString = connectionString, IsAutoCloseConnection = true, MoreSettings=new ConnMoreSettings() { PgSqlIsAutoToLower = false,//增删查改支持驼峰表 PgSqlIsAutoToLowerCodeFirst = false, // 建表建驼峰表。5.1.3.30 IsAutoToUpper=false //禁用自动转成大写表 } }); return sqlSugar; }); } 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(); }); } } }