using System.IO; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Extensions.DependencyInjection; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using WinIn.FasterZ.AuthSiteCenter.EntityFrameworkCore; using WinIn.FasterZ.AuthSiteCenter.Localization; using WinIn.FasterZ.AuthSiteCenter.MultiTenancy; using WinIn.FasterZ.AuthSiteCenter.Web.Menus; using Microsoft.OpenApi.Models; using OpenIddict.Validation.AspNetCore; using Volo.Abp; using Volo.Abp.Account.Web; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.Localization; using Volo.Abp.AspNetCore.Mvc.UI; using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap; using Volo.Abp.AspNetCore.Mvc.UI.Bundling; using Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy; using Volo.Abp.AspNetCore.Mvc.UI.Theme.LeptonXLite; using Volo.Abp.AspNetCore.Mvc.UI.Theme.LeptonXLite.Bundling; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared; using Volo.Abp.AspNetCore.Serilog; using Volo.Abp.Autofac; using Volo.Abp.AutoMapper; using Volo.Abp.Caching.StackExchangeRedis; using Volo.Abp.FeatureManagement; using Volo.Abp.Identity.Web; using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.PermissionManagement.Web; using Volo.Abp.SettingManagement.Web; using Volo.Abp.Swashbuckle; using Volo.Abp.TenantManagement.Web; using Volo.Abp.UI.Navigation.Urls; using Volo.Abp.UI; using Volo.Abp.UI.Navigation; using Volo.Abp.VirtualFileSystem; //using WinIn.FasterZ.Store; using Microsoft.AspNetCore.DataProtection; using StackExchange.Redis; using System; using System.Linq; using Volo.Abp.BackgroundJobs; using Volo.Abp.Caching; using Medallion.Threading; using Medallion.Threading.Redis; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Http.Features; using Volo.Abp.Authorization.Permissions; using WinIn.FasterZ.AuthSiteCenter.Permissions; //using WinIn.FasterZ.InterfaceDash; //using WinIn.FasterZ.Job; using Volo.Abp.AspNetCore.Mvc.AntiForgery; using Autofac.Core; namespace WinIn.FasterZ.AuthSiteCenter.Web; using WinIn.FasterZ.Wms; [DependsOn( typeof(AuthSiteCenterHttpApiModule), typeof(AuthSiteCenterApplicationModule), typeof(AuthSiteCenterEntityFrameworkCoreModule), typeof(AbpAutofacModule), typeof(AbpCachingStackExchangeRedisModule), typeof(AbpIdentityWebModule), typeof(AbpSettingManagementWebModule), typeof(AbpAccountWebOpenIddictModule), typeof(AbpAspNetCoreMvcUiLeptonXLiteThemeModule), typeof(AbpTenantManagementWebModule), typeof(AbpAspNetCoreSerilogModule), //----------权限添加 //typeof(StoreApplicationContractsModule), //typeof(JobApplicationContractsModule), //typeof(InterfaceDashApplicationContractsModule) typeof(WmsApplicationContractsModule), typeof(AbpSwashbuckleModule) )] public class AuthSiteCenterWebModule : AbpModule { public override void PreConfigureServices(ServiceConfigurationContext context) { context.Services.PreConfigure(options => { options.AddAssemblyResource( typeof(AuthSiteCenterResource), typeof(AuthSiteCenterDomainModule).Assembly, typeof(AuthSiteCenterDomainSharedModule).Assembly, typeof(AuthSiteCenterApplicationModule).Assembly, typeof(AuthSiteCenterApplicationContractsModule).Assembly, typeof(AuthSiteCenterWebModule).Assembly ); }); PreConfigure(builder => { builder.AddValidation(options => { //options.AddAudiences("AuthSiteCenter Wms"); options.AddAudiences("AuthSiteCenter Wms"); options.UseLocalServer(); options.UseAspNetCore(); }); }); Configure(options => { options.AutoValidate = false; }); Configure(options => { options.AddMaps(); }); RemoveOnlyHttps(context); } public override void ConfigureServices(ServiceConfigurationContext context) { var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); ConfigureAuthentication(context, configuration); ConfigureUrls(configuration); ConfigureBundles(); ConfigureAutoMapper(); ConfigureVirtualFileSystem(hostingEnvironment); ConfigureNavigationServices(); ConfigureAutoApiControllers(); ConfigureSwaggerServices(context.Services); Configure(options => { options.IsJobExecutionEnabled = false; }); Configure(options => { options.ValueCountLimit = 5000; // 5000 items max options.ValueLengthLimit = 1024 * 1024 * 100; // 100MB max len form data }); Configure(options => { options.KeyPrefix = "AuthSiteCenter:"; }); var dataProtectionBuilder = context.Services.AddDataProtection().SetApplicationName("AuthSiteCenter"); if (!hostingEnvironment.IsDevelopment()) { var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); dataProtectionBuilder.PersistKeysToStackExchangeRedis(redis, "AuthSiteCenter-Protection-Keys"); } context.Services.AddSingleton(sp => { var connection = ConnectionMultiplexer .Connect(configuration["Redis:Configuration"]); return new RedisDistributedSynchronizationProvider(connection.GetDatabase()); }); context.Services.AddCors(options => { options.AddDefaultPolicy(builder => { builder .WithOrigins( configuration["App:CorsOrigins"]? .Split(",", StringSplitOptions.RemoveEmptyEntries) .Select(o => o.RemovePostFix("/")) .ToArray() ?? Array.Empty() ) .WithAbpExposedHeaders() .SetIsOriginAllowedToAllowWildcardSubdomains() .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); } private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration) { context.Services.ForwardIdentityAuthenticationForBearer(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme); //自定义权限检查器 Configure(options => { options.ValueProviders.Clear(); options.ValueProviders.Add(); options.ValueProviders.Add(); options.ValueProviders.Add(); }); context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.Authority = configuration["AuthServer:Authority"]; options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]); options.Audience = "AuthSiteCenter"; }) .AddCookie("Cookies"); } private void ConfigureUrls(IConfiguration configuration) { Configure(options => { options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"]; options.RedirectAllowedUrls.AddRange(configuration["App:RedirectAllowedUrls"]?.Split(',') ?? Array.Empty()); }); } private void ConfigureBundles() { Configure(options => { options.StyleBundles.Configure( LeptonXLiteThemeBundles.Styles.Global, bundle => { bundle.AddFiles("/global-styles.css"); } ); }); } private void ConfigureAutoMapper() { Configure(options => { options.AddMaps(); }); } private void ConfigureVirtualFileSystem(IWebHostEnvironment hostingEnvironment) { if (hostingEnvironment.IsDevelopment()) { Configure(options => { options.FileSets.ReplaceEmbeddedByPhysical(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}WinIn.FasterZ.AuthSiteCenter.Domain.Shared")); options.FileSets.ReplaceEmbeddedByPhysical(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}WinIn.FasterZ.AuthSiteCenter.Domain")); options.FileSets.ReplaceEmbeddedByPhysical(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}WinIn.FasterZ.AuthSiteCenter.Application.Contracts")); options.FileSets.ReplaceEmbeddedByPhysical(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}WinIn.FasterZ.AuthSiteCenter.Application")); options.FileSets.ReplaceEmbeddedByPhysical(hostingEnvironment.ContentRootPath); }); } } private void ConfigureNavigationServices() { Configure(options => { options.MenuContributors.Add(new AuthSiteCenterMenuContributor()); }); } private void ConfigureAutoApiControllers() { Configure(options => { options.ConventionalControllers.Create(typeof(AuthSiteCenterApplicationModule).Assembly); }); } private void ConfigureSwaggerServices(IServiceCollection services) { services.AddAbpSwaggerGen( options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "AuthSiteCenter API", Version = "v1" }); options.DocInclusionPredicate((docName, description) => true); options.CustomSchemaIds(type => type.FullName); } ); } public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var env = context.GetEnvironment(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseAbpRequestLocalization(); if (!env.IsDevelopment()) { app.UseErrorPage(); } app.UseCorrelationId(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthenticationZ(); app.UseAbpOpenIddictValidation(); if (MultiTenancyConsts.IsEnabled) { app.UseMultiTenancy(); } app.UseUnitOfWork(); app.UseAuthorization(); app.UseCors(); app.UseSwagger(); app.UseAbpSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "AuthSiteCenter API"); }); app.UseAuditing(); app.UseAbpSerilogEnrichers(); app.UseConfiguredEndpoints(); } /// /// 去掉只允许https的限制 /// /// /// private OpenIddictBuilder RemoveOnlyHttps(ServiceConfigurationContext context) { return context.Services.AddOpenIddict() .AddServer(option => { option.SetAccessTokenLifetime(TimeSpan.FromSeconds(7200)); option.AllowPasswordFlow(); option.AllowRefreshTokenFlow(); option.UseAspNetCore() .DisableTransportSecurityRequirement(); }); } }