Files
Yi.Admin/CC.Yi/CC.Yi.API/Startup.cs

168 lines
5.6 KiB
C#
Raw Normal View History

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 =>
{
//<2F><><EFBFBD>û<EFBFBD><C3BB>ڲ<EFBFBD><DAB2>Ե<EFBFBD><D4B5><EFBFBD>֤
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,//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>֤Issuer
ValidateAudience = true,//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>֤Audience
ValidateLifetime = true,//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>֤ʧЧʱ<D0A7><CAB1>
2021-06-02 20:00:25 +08:00
ClockSkew = TimeSpan.FromDays(1),
2021-05-13 01:39:34 +08:00
ValidateIssuerSigningKey = true,//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>֤SecurityKey
ValidAudience = JwtConst.Domain,//Audience
ValidIssuer = JwtConst.Domain,//Issuer<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>ǩ<EFBFBD><C7A9>jwt<77><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtConst.SecurityKey))//<2F>õ<EFBFBD>SecurityKey
};
});
2021-06-02 20:00:25 +08:00
//ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
2021-05-13 01:39:34 +08:00
//<2F><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD>
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();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
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<71>
2021-03-20 14:12:24 +08:00
services.AddDbContext<DataContext>(options =>
{
2021-06-02 20:00:25 +08:00
//options.UseSqlServer(connection1);//sqlserver<65><72><EFBFBD><EFBFBD>
//options.UseMySql(connection2, serverVersion);//mysql<71><6C><EFBFBD><EFBFBD>
options.UseSqlite(connection3);//sqlite<74><65><EFBFBD><EFBFBD>
//options.UseOracle(connection4);//oracle<6C><65><EFBFBD><EFBFBD>
2021-03-20 14:12:24 +08:00
});
2021-05-13 01:39:34 +08:00
services.AddCors(options => options.AddPolicy("CorsPolicy",//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
builder =>
{
builder.AllowAnyMethod()
.SetIsOriginAllowed(_ => true)
.AllowAnyHeader()
.AllowCredentials();
}));
}
//<2F><>ʼ<EFBFBD><CABC>ʹ<EFBFBD>ú<EFBFBD><C3BA><EFBFBD>
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("<22><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD>");
}
}
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
//<2F><><EFBFBD>ÿ<EFBFBD><C3BF>ӻ<EFBFBD><D3BB>ӿ<EFBFBD>
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
//<2F><><EFBFBD>þ<EFBFBD>̬<EFBFBD>ļ<EFBFBD>
app.UseStaticFiles();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3A3>׽
app.UseErrorHandling();
2021-03-20 14:12:24 +08:00
2021-06-02 20:00:25 +08:00
//<2F><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤
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
//<2F><>ʼ<EFBFBD><CABC>
2021-05-13 01:39:34 +08:00
InitData(app.ApplicationServices);
2021-03-20 14:12:24 +08:00
}
}
}