2021-05-13 01:39:34 +08:00
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Extras.DynamicProxy;
|
|
|
|
|
using CC.Yi.API.Extension;
|
2021-03-20 14:12:24 +08:00
|
|
|
using CC.Yi.BLL;
|
2021-05-13 01:39:34 +08:00
|
|
|
using CC.Yi.Common.Castle;
|
2021-06-02 20:00:25 +08:00
|
|
|
using CC.Yi.Common.Json;
|
2021-05-13 01:39:34 +08:00
|
|
|
using CC.Yi.Common.Jwt;
|
2021-03-20 14:12:24 +08:00
|
|
|
using CC.Yi.DAL;
|
|
|
|
|
using CC.Yi.IBLL;
|
|
|
|
|
using CC.Yi.IDAL;
|
|
|
|
|
using CC.Yi.Model;
|
2021-05-13 01:39:34 +08:00
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2021-03-20 14:12:24 +08:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2021-06-02 20:00:25 +08:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-03-20 14:12:24 +08:00
|
|
|
using Microsoft.AspNetCore.HttpsPolicy;
|
2021-05-13 01:39:34 +08:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2021-03-20 14:12:24 +08:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-05-13 01:39:34 +08:00
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2021-03-20 14:12:24 +08:00
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-05-13 01:39:34 +08:00
|
|
|
using System.Text;
|
2021-03-20 14:12:24 +08:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace CC.Yi.API
|
|
|
|
|
{
|
|
|
|
|
public partial 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-13 01:39:34 +08:00
|
|
|
services.AddAuthorization(options =>
|
|
|
|
|
{
|
|
|
|
|
//配置基于策略的验证
|
2021-06-02 20:00:25 +08:00
|
|
|
options.AddPolicy("myadmin", policy =>policy.RequireRole("admin"));
|
|
|
|
|
|
2021-05-13 01:39:34 +08:00
|
|
|
});
|
2021-03-20 14:12:24 +08:00
|
|
|
|
2021-05-13 01:39:34 +08:00
|
|
|
|
|
|
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
2021-06-02 20:00:25 +08:00
|
|
|
.AddJwtBearer(options =>
|
|
|
|
|
{
|
2021-05-13 01:39:34 +08:00
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
ValidateIssuer = true,//是否验证Issuer
|
|
|
|
|
ValidateAudience = true,//是否验证Audience
|
|
|
|
|
ValidateLifetime = true,//是否验证失效时间
|
2021-06-02 20:00:25 +08:00
|
|
|
ClockSkew = TimeSpan.FromDays(1),
|
|
|
|
|
|
|
|
|
|
|
2021-05-13 01:39:34 +08:00
|
|
|
ValidateIssuerSigningKey = true,//是否验证SecurityKey
|
|
|
|
|
ValidAudience = JwtConst.Domain,//Audience
|
|
|
|
|
ValidIssuer = JwtConst.Domain,//Issuer,这两项和前面签发jwt的设置一致
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtConst.SecurityKey))//拿到SecurityKey
|
|
|
|
|
};
|
|
|
|
|
});
|
2021-06-02 20:00:25 +08:00
|
|
|
|
|
|
|
|
//注入上下文对象
|
|
|
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
2021-05-13 01:39:34 +08:00
|
|
|
|
|
|
|
|
//配置过滤器
|
2021-06-02 20:00:25 +08:00
|
|
|
Action<MvcOptions> filters = new Action<MvcOptions>(r =>
|
|
|
|
|
{
|
2021-05-13 01:39:34 +08:00
|
|
|
//r.Filters.Add(typeof(DbContextFilter));
|
2021-03-20 14:12:24 +08:00
|
|
|
});
|
2021-06-02 20:00:25 +08:00
|
|
|
|
|
|
|
|
services.AddControllers(filters).AddJsonOptions(options => {
|
|
|
|
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new TimeSpanJsonConverter());
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
services.AddSwaggerService();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//配置数据库连接
|
2021-05-13 01:39:34 +08:00
|
|
|
string connection1 = Configuration["ConnectionStringBySQL"];
|
2021-03-20 14:12:24 +08:00
|
|
|
string connection2 = Configuration["ConnectionStringByMySQL"];
|
2021-05-13 01:39:34 +08:00
|
|
|
string connection3 = Configuration["ConnectionStringBySQLite"];
|
2021-06-02 20:00:25 +08:00
|
|
|
string connection4 = Configuration["ConnectionStringByOracle"];
|
|
|
|
|
|
|
|
|
|
//var serverVersion = new MySqlServerVersion(new Version(8, 0, 21));//mysql版本
|
|
|
|
|
|
2021-03-20 14:12:24 +08:00
|
|
|
services.AddDbContext<DataContext>(options =>
|
|
|
|
|
{
|
2021-06-02 20:00:25 +08:00
|
|
|
//options.UseSqlServer(connection1);//sqlserver连接
|
|
|
|
|
//options.UseMySql(connection2, serverVersion);//mysql连接
|
|
|
|
|
options.UseSqlite(connection3);//sqlite连接
|
|
|
|
|
//options.UseOracle(connection4);//oracle连接
|
2021-03-20 14:12:24 +08:00
|
|
|
});
|
2021-05-13 01:39:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
services.AddCors(options => options.AddPolicy("CorsPolicy",//解决跨域问题
|
|
|
|
|
builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.AllowAnyMethod()
|
|
|
|
|
.SetIsOriginAllowed(_ => true)
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowCredentials();
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//初始化使用函数
|
|
|
|
|
private void InitData(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
2021-06-02 20:00:25 +08:00
|
|
|
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var Db = serviceScope.ServiceProvider.GetService<DataContext>();
|
|
|
|
|
var log = serviceScope.ServiceProvider.GetService<Logger<string>>();
|
|
|
|
|
if (Init.InitDb.Init(Db))
|
|
|
|
|
{
|
|
|
|
|
log.LogInformation("数据库初始化成功!");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-20 14:12:24 +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();
|
2021-06-02 20:00:25 +08:00
|
|
|
|
|
|
|
|
//配置可视化接口
|
2021-05-13 01:39:34 +08:00
|
|
|
app.UseSwaggerService();
|
2021-03-20 14:12:24 +08:00
|
|
|
}
|
2021-06-02 20:00:25 +08:00
|
|
|
//配置静态文件
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
|
|
|
|
//配置异常捕捉
|
|
|
|
|
app.UseErrorHandling();
|
2021-03-20 14:12:24 +08:00
|
|
|
|
2021-06-02 20:00:25 +08:00
|
|
|
//配置跨域问题
|
2021-05-13 01:39:34 +08:00
|
|
|
app.UseCors("CorsPolicy");
|
2021-03-20 14:12:24 +08:00
|
|
|
app.UseHttpsRedirection();
|
2021-06-02 20:00:25 +08:00
|
|
|
|
2021-03-20 14:12:24 +08:00
|
|
|
app.UseRouting();
|
2021-06-02 20:00:25 +08:00
|
|
|
|
|
|
|
|
//配置身份验证
|
2021-05-13 01:39:34 +08:00
|
|
|
app.UseAuthentication();
|
2021-03-20 14:12:24 +08:00
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
{
|
|
|
|
|
endpoints.MapControllers();
|
|
|
|
|
});
|
2021-06-02 20:00:25 +08:00
|
|
|
|
|
|
|
|
//初始化
|
2021-05-13 01:39:34 +08:00
|
|
|
InitData(app.ApplicationServices);
|
2021-03-20 14:12:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|