231 lines
12 KiB
C#
231 lines
12 KiB
C#
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<IHttpContextAccessor, HttpContextAccessor>();
|
||
services.AddHttpContextAccessor();
|
||
|
||
services.Configure<CookiePolicyOptions>(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<FormOptions>(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<crmContext>(d => d.UseMySQL(Configuration.GetConnectionString("crmContext")));
|
||
services.AddDbContext<wxContext>(d => d.UseMySQL(Configuration.GetConnectionString("wxContext")));
|
||
//services.AddScoped<DbContext, crmContext>();
|
||
services.Add(new ServiceDescriptor(typeof(DbContext), typeof(crmContext), ServiceLifetime.Scoped));
|
||
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_InnerUserRole>), typeof(EfAdminRepository<Bas_InnerUserRole>), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_InnerUser>), typeof(EfAdminRepository<Bas_InnerUser>), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_InnerUserSalt>), typeof(EfAdminRepository<Bas_InnerUserSalt>), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_LeftMemu>), typeof(EfAdminRepository<Bas_LeftMemu>), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_ModuleMenu>), typeof(EfAdminRepository<Bas_ModuleMenu>), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_Right>), typeof(EfAdminRepository<Bas_Right>), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_RightGroup>), typeof(EfAdminRepository<Bas_RightGroup>), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_Right_ToolButton>), typeof(EfAdminRepository<Bas_Right_ToolButton>), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_Role>), typeof(EfAdminRepository<Bas_Role>), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_RoleRightResource>), typeof(EfAdminRepository<Bas_RoleRightResource>), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_Supplier>), typeof(EfAdminRepository<Bas_Supplier>), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(ILoginHelper), typeof(LoginHelper), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_Config>), typeof(EfAdminRepository<Bas_Config>), ServiceLifetime.Scoped));
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_Parameter>), typeof(EfAdminRepository<Bas_Parameter>), ServiceLifetime.Scoped));
|
||
|
||
services.Add(new ServiceDescriptor(typeof(IAdminRepository<Bas_LoginLog>), typeof(EfAdminRepository<Bas_LoginLog>), ServiceLifetime.Scoped));
|
||
|
||
|
||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||
|
||
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<IBasInnerUserRoleService, BasInnerUserRoleService>();
|
||
services.AddScoped<IBasInnerUserSaltService, BasInnerUserSaltService>();
|
||
services.AddScoped<IBasInnerUserService, BasInnerUserService>();
|
||
services.AddScoped<IBasLeftMenuService, BasLeftMenuService>();
|
||
services.AddScoped<IBasModuleMenuService, BasModuleMenuService>();
|
||
services.AddScoped<IBasRightGroupService, BasRightGroupService>();
|
||
services.AddScoped<IBasRightService, BasRightService>();
|
||
services.AddScoped<IBasRightToolButtonService, BasRightToolButtonService>();
|
||
services.AddScoped<IBasRoleRightResourceService, BasRoleRightResourceService>();
|
||
services.AddScoped<IBasRoleService, BasRoleService>();
|
||
services.AddScoped<IBasSupplierService, BasSupplierService>();
|
||
services.AddScoped<Iww_hhuser_Service, ww_hhuser_Service>();
|
||
services.AddScoped<Iww_QDhhuser_Service, ww_QDhhuser_Service>();
|
||
services.AddScoped<IBasConfigService, BasConfigService>();
|
||
services.AddScoped<ICacheService, CacheService>();
|
||
services.AddScoped<IBasParameterService, BasParameterService>();
|
||
|
||
services.AddScoped<Iwx_workwechat_Service, wx_workwechat_Service>();
|
||
services.AddScoped<Ibas_parameter_Service, bas_parameter_Service>();
|
||
services.AddScoped<IBasLoginLogService, BasLoginLogService>();
|
||
|
||
}
|
||
// 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<AutofacModuleRegister>();
|
||
//创建容器
|
||
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<BasInnerUserRoleService>().Named<IBasInnerUserRoleService>(typeof(BasInnerUserRoleService).Name);
|
||
builder.RegisterType<BasRoleRightResourceService>().Named<IBasRoleRightResourceService>(typeof(BasRoleRightResourceService).Name);
|
||
builder.RegisterType<BasInnerUserService>().Named<IBasInnerUserService>(typeof(BasInnerUserService).Name);
|
||
builder.RegisterType<BasRoleService>().Named<IBasRoleService>(typeof(BasRoleService).Name);
|
||
}
|
||
/// <summary>
|
||
/// 根据程序集名称获取程序集
|
||
/// </summary>
|
||
/// <param name="AssemblyName">程序集名称</param>
|
||
/// <returns></returns>
|
||
public static Assembly GetAssemblyByName(String AssemblyName)
|
||
{
|
||
return Assembly.Load(AssemblyName);
|
||
}
|
||
}
|
||
}
|