This commit is contained in:
454313500@qq.com
2021-04-10 18:28:45 +08:00
parent d26140ac18
commit 8673e8fd85
12 changed files with 386 additions and 318 deletions

View File

@@ -38,50 +38,62 @@ namespace CC.Yi.API
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "CC.Yi.API", Version = "v1" });
});
string connection = Configuration["ConnectionStringBySQL"];
services.AddSession();
services.AddMvc();
string connection1 = Configuration["ConnectionStringBySQL"];
string connection2 = Configuration["ConnectionStringByMySQL"];
string connection3 = Configuration["ConnectionStringBySQLite"];
services.AddDbContext<DataContext>(options =>
{
options.UseSqlServer(connection, b => b.MigrationsAssembly("CC.Yi.API"));//设置数据库
options.UseSqlServer(connection1, b => b.MigrationsAssembly("CC.Yi.API"));//设置数据库
});
//依赖注入转交给Autofac
//services.AddScoped(typeof(IBaseDal<>), typeof(BaseDal<>));
//services.AddScoped(typeof(IstudentBll), typeof(studentBll));
services.AddSingleton(typeof(ICacheWriter), new RedisCacheService(new Microsoft.Extensions.Caching.Redis.RedisCacheOptions()
{
Configuration = Configuration.GetSection("Cache.ConnectionString").Value,
InstanceName = Configuration.GetSection("Cache.InstanceName").Value
}));
//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
//}));
//配置Identity身份认证
services.AddIdentity<result_user, IdentityRole>(options =>
//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();
services.AddCors(options => options.AddPolicy("CorsPolicy",//解决跨域问题
builder =>
{
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();
builder.AllowAnyMethod()
.SetIsOriginAllowed(_ => true)
.AllowAnyHeader()
.AllowCredentials();
}));
}
private void InitData(IServiceProvider serviceProvider)
{
var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
var context = serviceScope.ServiceProvider.GetService<DataContext>();
DbContentFactory.Initialize(context);//调用静态类方法注入
var context = serviceScope.ServiceProvider.GetService<DataContext>();
DbContentFactory.Initialize(context);//调用静态类方法注入
}
@@ -89,7 +101,7 @@ namespace CC.Yi.API
// 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();
@@ -97,10 +109,10 @@ namespace CC.Yi.API
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "CC.Yi.API v1"));
}
app.UseAuthentication();
//app.UseAuthentication();
app.UseCors("CorsPolicy");
app.UseHttpsRedirection();
app.UseSession();
app.UseRouting();
app.UseAuthorization();