You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
12 KiB

2 years ago
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;
2 years ago
//using WinIn.FasterZ.Job;
using Volo.Abp.AspNetCore.Mvc.AntiForgery;
using Autofac.Core;
namespace WinIn.FasterZ.AuthSiteCenter.Web;
using WinIn.FasterZ.Wms;
2 years ago
[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),
2 years ago
typeof(AbpSwashbuckleModule)
)]
public class AuthSiteCenterWebModule : AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
context.Services.PreConfigure<AbpMvcDataAnnotationsLocalizationOptions>(options =>
{
options.AddAssemblyResource(
typeof(AuthSiteCenterResource),
typeof(AuthSiteCenterDomainModule).Assembly,
typeof(AuthSiteCenterDomainSharedModule).Assembly,
typeof(AuthSiteCenterApplicationModule).Assembly,
typeof(AuthSiteCenterApplicationContractsModule).Assembly,
typeof(AuthSiteCenterWebModule).Assembly
);
});
PreConfigure<OpenIddictBuilder>(builder =>
{
builder.AddValidation(options =>
{
//options.AddAudiences("AuthSiteCenter Wms");
options.AddAudiences("AuthSiteCenter Wms");
2 years ago
options.UseLocalServer();
options.UseAspNetCore();
});
});
Configure<AbpAntiForgeryOptions>(options =>
{
options.AutoValidate = false;
});
Configure<AbpAutoMapperOptions>(options =>
{
options.AddMaps<AuthSiteCenterAutoMapperProfile>();
});
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<AbpBackgroundJobOptions>(options =>
{
options.IsJobExecutionEnabled = false;
});
Configure<FormOptions>(options =>
{
options.ValueCountLimit = 5000; // 5000 items max
options.ValueLengthLimit = 1024 * 1024 * 100; // 100MB max len form data
});
Configure<AbpDistributedCacheOptions>(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<IDistributedLockProvider>(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<string>()
)
.WithAbpExposedHeaders()
.SetIsOriginAllowedToAllowWildcardSubdomains()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
}
private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.ForwardIdentityAuthenticationForBearer(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme);
//�Զ���Ȩ�޼�����
Configure<AbpPermissionOptions>(options =>
{
options.ValueProviders.Clear();
options.ValueProviders.Add<UserPermissionValueProvider>();
options.ValueProviders.Add<ZRolePermissionValueProvider>();
options.ValueProviders.Add<ClientPermissionValueProvider>();
});
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<AppUrlOptions>(options =>
{
options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"];
options.RedirectAllowedUrls.AddRange(configuration["App:RedirectAllowedUrls"]?.Split(',') ?? Array.Empty<string>());
});
}
private void ConfigureBundles()
{
Configure<AbpBundlingOptions>(options =>
{
options.StyleBundles.Configure(
LeptonXLiteThemeBundles.Styles.Global,
bundle =>
{
bundle.AddFiles("/global-styles.css");
}
);
});
}
private void ConfigureAutoMapper()
{
Configure<AbpAutoMapperOptions>(options =>
{
options.AddMaps<AuthSiteCenterWebModule>();
});
}
private void ConfigureVirtualFileSystem(IWebHostEnvironment hostingEnvironment)
{
if (hostingEnvironment.IsDevelopment())
{
Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.ReplaceEmbeddedByPhysical<AuthSiteCenterDomainSharedModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}WinIn.FasterZ.AuthSiteCenter.Domain.Shared"));
options.FileSets.ReplaceEmbeddedByPhysical<AuthSiteCenterDomainModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}WinIn.FasterZ.AuthSiteCenter.Domain"));
options.FileSets.ReplaceEmbeddedByPhysical<AuthSiteCenterApplicationContractsModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}WinIn.FasterZ.AuthSiteCenter.Application.Contracts"));
options.FileSets.ReplaceEmbeddedByPhysical<AuthSiteCenterApplicationModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}WinIn.FasterZ.AuthSiteCenter.Application"));
options.FileSets.ReplaceEmbeddedByPhysical<AuthSiteCenterWebModule>(hostingEnvironment.ContentRootPath);
});
}
}
private void ConfigureNavigationServices()
{
Configure<AbpNavigationOptions>(options =>
{
options.MenuContributors.Add(new AuthSiteCenterMenuContributor());
});
}
private void ConfigureAutoApiControllers()
{
Configure<AbpAspNetCoreMvcOptions>(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();
}
/// <summary>
/// ȥ��ֻ����https������
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private OpenIddictBuilder RemoveOnlyHttps(ServiceConfigurationContext context)
{
return context.Services.AddOpenIddict()
.AddServer(option =>
{
option.SetAccessTokenLifetime(TimeSpan.FromSeconds(7200));
option.AllowPasswordFlow();
option.AllowRefreshTokenFlow();
option.UseAspNetCore()
.DisableTransportSecurityRequirement();
});
}
}