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

165 lines
5.8 KiB
C#
Raw Normal View History

2021-05-31 21:41:27 +08:00
2021-05-13 01:39:34 +08:00
using Autofac;
using Autofac.Extras.DynamicProxy;
using CC.Yi.API.Extension;
2021-05-31 21:41:27 +08:00
using CC.Yi.API.Filter;
2021-03-20 14:12:24 +08:00
using CC.Yi.BLL;
2021-05-31 21:41:27 +08:00
using CC.Yi.Common.Cache;
2021-05-13 01:39:34 +08:00
using CC.Yi.Common.Castle;
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;
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; }
2021-05-31 21:41:27 +08:00
2021-03-20 14:12:24 +08:00
public void ConfigureServices(IServiceCollection services)
{
2021-05-31 21:41:27 +08:00
// 配置Jwt
2021-05-13 01:39:34 +08:00
services.AddAuthorization(options =>
{
//配置基于策略的验证
options.AddPolicy("myadmin", policy =>
policy.RequireRole("admin"));
});
2021-03-20 14:12:24 +08:00
2021-05-31 21:41:27 +08:00
2021-05-13 01:39:34 +08:00
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,//是否验证Issuer
ValidateAudience = true,//是否验证Audience
ValidateLifetime = true,//是否验证失效时间
ClockSkew = TimeSpan.FromSeconds(30),
ValidateIssuerSigningKey = true,//是否验证SecurityKey
ValidAudience = JwtConst.Domain,//Audience
ValidIssuer = JwtConst.Domain,//Issuer,这两项和前面签发jwt的设置一致
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtConst.SecurityKey))//拿到SecurityKey
};
});
2021-05-31 21:41:27 +08:00
2021-03-20 14:12:24 +08:00
services.AddControllers();
2021-05-13 01:39:34 +08:00
services.AddSwaggerService();
services.AddSession();
2021-05-31 21:41:27 +08:00
2021-05-13 01:39:34 +08:00
//配置过滤器
Action<MvcOptions> filters = new Action<MvcOptions>(r => {
//r.Filters.Add(typeof(DbContextFilter));
2021-03-20 14:12:24 +08:00
});
2021-05-13 01:39:34 +08:00
services.AddMvc(filters);
2021-05-31 21:41:27 +08:00
//配置数据库连接
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-03-20 14:12:24 +08:00
services.AddDbContext<DataContext>(options =>
{
2021-05-31 21:41:27 +08:00
options.UseSqlServer(connection1, b => b.MigrationsAssembly("CC.Yi.API"));//设置数据库
2021-03-20 14:12:24 +08:00
});
2021-05-13 01:39:34 +08:00
//依赖注入转交给Autofac
//services.AddScoped(typeof(IBaseDal<>), typeof(BaseDal<>));
//services.AddScoped(typeof(IstudentBll), typeof(studentBll));
2021-05-31 21:41:27 +08:00
//reids注册
//services.AddSingleton(typeof(ICacheWriter), new RedisCacheService(new Microsoft.Extensions.Caching.Redis.RedisCacheOptions()
//{
// Configuration = Configuration.GetSection("Cache.ConnectionString").Value,
// InstanceName = Configuration.GetSection("Cache.InstanceName").Value
//}));
2021-05-13 01:39:34 +08:00
//配置Identity身份认证
//services.AddIdentity<result_user, IdentityRole>(options =>
// {
// options.Password.RequiredLength = 6;//密码最短长度
// options.Password.RequireDigit = false;//密码需求数字
// options.Password.RequireLowercase = false;//密码需求小写字母
// options.Password.RequireNonAlphanumeric = false;//密码需求特殊字符
// options.Password.RequireUppercase = false;//密码需求大写字母
// //options.User.RequireUniqueEmail = false;//注册邮箱是否可以不重复
// //options.User.AllowedUserNameCharacters="abcd"//密码只允许在这里的字符
//}).AddEntityFrameworkStores<DataContext>().AddDefaultTokenProviders();
2021-05-31 21:41:27 +08:00
//解决跨域问题
services.AddCors(options => options.AddPolicy("CorsPolicy",
builder =>
{
builder.AllowAnyMethod()
.SetIsOriginAllowed(_ => true)
.AllowAnyHeader()
.AllowCredentials();
}));
2021-05-13 01:39:34 +08:00
}
//初始化使用函数
private void InitData(IServiceProvider serviceProvider)
{
//var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
//var context = serviceScope.ServiceProvider.GetService<DataContext>();
//DbContentFactory.Initialize(context);//调用静态类方法注入
2021-03-20 14:12:24 +08:00
}
2021-05-31 21:41:27 +08:00
2021-03-20 14:12:24 +08:00
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
2021-05-31 21:41:27 +08:00
2021-03-20 14:12:24 +08:00
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
2021-05-13 01:39:34 +08:00
app.UseSwaggerService();
2021-03-20 14:12:24 +08:00
}
2021-05-31 21:41:27 +08:00
//app.UseAuthentication();
2021-05-13 01:39:34 +08:00
app.UseCors("CorsPolicy");
2021-03-20 14:12:24 +08:00
app.UseHttpsRedirection();
2021-05-13 01:39:34 +08:00
app.UseSession();
2021-03-20 14:12:24 +08:00
app.UseRouting();
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-05-13 01:39:34 +08:00
InitData(app.ApplicationServices);
2021-03-20 14:12:24 +08:00
}
}
}