using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; using Autofac; using Autofac.Extensions.DependencyInjection; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Internal; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Mini.Common; using Mini.Model; using Mini.Model.Entity; using Mini.Services; using Mini.Services.Bas; using Mini.Services.ww; using Mini.Services.wx; using Mini.Web.WebHelper; namespace Mini.Web { public 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) { services.AddSingleton(); services.AddHttpContextAccessor(); services.Configure(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.Lax; }); services.Configure(options => { options.ValueCountLimit = 5000; // 5000 items max options.ValueLengthLimit = 1024 * 1024 * 100; // 100MB max len form data }); services.AddAuthentication(option => { option.DefaultScheme = "MyCookies"; option.DefaultChallengeScheme = "MyCookies"; option.DefaultAuthenticateScheme = "MyCookies"; option.DefaultForbidScheme = "MyCookies"; option.DefaultSignInScheme = "MyCookies"; option.DefaultSignOutScheme = "MyCookies"; }).AddCookie("MyCookies", x => { //x.DataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "/shared-auth-ticket-keys/")); x.Cookie.Name = "MyCookies"; x.Cookie.Path = "/"; //x.Cookie.Expiration = TimeSpan.FromHours(3); x.LoginPath = new PathString("/Admin/Account/Login");//登录页面 x.LogoutPath = new PathString("/Admin/Account/LogOff");//登出页面 x.AccessDeniedPath = "/Admin/Account/AccessDenied"; }); //services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => options.LoginPath = new PathString("/Admin/Account/LogOn")); services.AddDbContext(d => d.UseMySQL(Configuration.GetConnectionString("crmContext"))); services.AddDbContext(d => d.UseMySQL(Configuration.GetConnectionString("wxContext"))); //services.AddScoped(); services.Add(new ServiceDescriptor(typeof(DbContext), typeof(crmContext), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(ILoginHelper), typeof(LoginHelper), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(IAdminRepository), typeof(EfAdminRepository), ServiceLifetime.Scoped)); services.AddSingleton(); RegisterService(services);//注册Service services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); //ContainerManager.InitContainer(); //services.AddAuthorization(); //Form基础验证 //services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) //.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o => //{ // o.Cookie.Name = "_AdminTicketCookie"; // o.LoginPath = new PathString("/Admin/Account/Login"); // o.LogoutPath = new PathString("/Admin/Account/LogOff"); // o.AccessDeniedPath = new PathString("/Error/Forbidden"); //}); //return RegisterAutofac(services);//注册Autofac new ww_hhuser_Service(null).InitDb(); } public void RegisterService(IServiceCollection services) { services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider svp, IHttpContextAccessor accessor, ILoginHelper loginHelper) { //MyHttpContext.ServiceProvider = svp; MvcContext.Accessor = accessor; MvcContext.loginhelper = loginHelper; if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseAuthentication(); app.UseHttpsRedirection(); app.UseCookiePolicy(); app.UseStaticFiles(); app.UseIdentity(); app.UseMvc(routes => { routes.MapRoute( name: "areaname", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); routes.MapRoute( name: "Admin", template: "Admin/{controller}/{action=Index}/{id?}"); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } private IServiceProvider RegisterAutofac(IServiceCollection services) { //实例化Autofac容器 var builder = new ContainerBuilder(); //将Services中的服务填充到Autofac中 builder.Populate(services); //新模块组件注册 builder.RegisterModule(); //创建容器 var Container = builder.Build(); //第三方IOC接管 core内置DI容器 return new AutofacServiceProvider(Container); } } public class AutofacModuleRegister : Autofac.Module { //重写Autofac管道Load方法,在这里注册注入 protected override void Load(ContainerBuilder builder) { //注册Service中的对象,Service中的类要以Service结尾,否则注册失败 builder.RegisterAssemblyTypes(GetAssemblyByName("Mini.Services")).Where(a => a.Name.EndsWith("Service")).AsImplementedInterfaces(); //注册Repository中的对象,Repository中的类要以Repository结尾,否则注册失败 //builder.RegisterAssemblyTypes(GetAssemblyByName("WXL.Repository")).Where(a => a.Name.EndsWith("Repository")).AsImplementedInterfaces(); builder.RegisterType().Named(typeof(BasInnerUserRoleService).Name); builder.RegisterType().Named(typeof(BasRoleRightResourceService).Name); builder.RegisterType().Named(typeof(BasInnerUserService).Name); builder.RegisterType().Named(typeof(BasRoleService).Name); } /// /// 根据程序集名称获取程序集 /// /// 程序集名称 /// public static Assembly GetAssemblyByName(String AssemblyName) { return Assembly.Load(AssemblyName); } } }