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;
|
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
|
|
|
|
// 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>֤
|
|
|
|
|
|
options.AddPolicy("myadmin", policy =>
|
|
|
|
|
|
policy.RequireRole("admin"));
|
|
|
|
|
|
});
|
2021-03-20 14:12:24 +08:00
|
|
|
|
|
2021-05-13 01:39:34 +08:00
|
|
|
|
|
|
|
|
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
|
.AddJwtBearer(options => {
|
|
|
|
|
|
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>
|
|
|
|
|
|
ClockSkew = TimeSpan.FromSeconds(30),
|
|
|
|
|
|
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-03-20 14:12:24 +08:00
|
|
|
|
services.AddControllers();
|
2021-05-13 01:39:34 +08:00
|
|
|
|
services.AddSwaggerService();
|
|
|
|
|
|
services.AddSession();
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
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);
|
|
|
|
|
|
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-13 01:39:34 +08:00
|
|
|
|
options.UseSqlite(connection3, b => b.MigrationsAssembly("CC.Yi.API"));//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>
|
2021-03-20 14:12:24 +08:00
|
|
|
|
});
|
2021-05-13 01:39:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>Autofac
|
|
|
|
|
|
//services.AddScoped(typeof(IBaseDal<>), typeof(BaseDal<>));
|
|
|
|
|
|
//services.AddScoped(typeof(IstudentBll), typeof(studentBll));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD><EFBFBD>Identity<74><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤
|
|
|
|
|
|
//services.AddIdentity<result_user, IdentityRole>(options =>
|
|
|
|
|
|
// {
|
|
|
|
|
|
// options.Password.RequiredLength = 6;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳<EFBFBD><CCB3><EFBFBD>
|
|
|
|
|
|
// options.Password.RequireDigit = false;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
// options.Password.RequireLowercase = false;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сд<D0A1><D0B4>ĸ
|
|
|
|
|
|
// options.Password.RequireNonAlphanumeric = false;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
|
|
|
|
|
|
// options.Password.RequireUppercase = false;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>ĸ
|
|
|
|
|
|
// //options.User.RequireUniqueEmail = false;//ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>Բ<EFBFBD><D4B2>ظ<EFBFBD>
|
|
|
|
|
|
// //options.User.AllowedUserNameCharacters="abcd"//<2F><><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
|
|
|
|
|
|
//}).AddEntityFrameworkStores<DataContext>().AddDefaultTokenProviders();
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
//var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
|
|
|
|
|
|
|
|
|
|
|
//var context = serviceScope.ServiceProvider.GetService<DataContext>();
|
|
|
|
|
|
//DbContentFactory.Initialize(context);//<2F><><EFBFBD>þ<EFBFBD>̬<EFBFBD><EFBFBD><E0B7BD>ע<EFBFBD><D7A2>
|
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-05-13 01:39:34 +08:00
|
|
|
|
app.UseSwaggerService();
|
2021-03-20 14:12:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|