mirror of https://github.com/abpframework/abp
				
				
				
			
			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.
		
		
		
		
		
			
		
			
				
					
					
						
							248 lines
						
					
					
						
							8.3 KiB
						
					
					
				
			
		
		
	
	
							248 lines
						
					
					
						
							8.3 KiB
						
					
					
				| using System;
 | |
| using Microsoft.AspNetCore.Builder;
 | |
| using Microsoft.AspNetCore.Hosting;
 | |
| using Microsoft.Extensions.DependencyInjection;
 | |
| using Microsoft.OpenApi.Models;
 | |
| using System.IO;
 | |
| using Microsoft.AspNetCore.Authentication;
 | |
| using Microsoft.AspNetCore.DataProtection;
 | |
| using Microsoft.Extensions.Configuration;
 | |
| using Microsoft.Extensions.Hosting;
 | |
| using Microsoft.IdentityModel.Protocols.OpenIdConnect;
 | |
| using Volo.CmsKit.Localization;
 | |
| using Volo.CmsKit.MultiTenancy;
 | |
| using Volo.CmsKit.Web;
 | |
| using StackExchange.Redis;
 | |
| using Volo.Abp;
 | |
| using Volo.Abp.AspNetCore.Authentication.OAuth;
 | |
| using Volo.Abp.AspNetCore.Mvc.Client;
 | |
| 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.Theme.Basic;
 | |
| 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;
 | |
| using Volo.Abp.Caching.StackExchangeRedis;
 | |
| using Volo.Abp.FeatureManagement;
 | |
| using Volo.Abp.Http.Client.IdentityModel.Web;
 | |
| using Volo.Abp.Identity;
 | |
| using Volo.Abp.Identity.Web;
 | |
| using Volo.Abp.Modularity;
 | |
| using Volo.Abp.MultiTenancy;
 | |
| using Volo.Abp.PermissionManagement;
 | |
| using Volo.Abp.PermissionManagement.Web;
 | |
| using Volo.Abp.Security.Claims;
 | |
| using Volo.Abp.TenantManagement;
 | |
| using Volo.Abp.TenantManagement.Web;
 | |
| using Volo.Abp.UI.Navigation.Urls;
 | |
| using Volo.Abp.UI;
 | |
| using Volo.Abp.UI.Navigation;
 | |
| using Volo.Abp.VirtualFileSystem;
 | |
| 
 | |
| namespace Volo.CmsKit;
 | |
| 
 | |
| [DependsOn(
 | |
|     typeof(CmsKitWebModule),
 | |
|     typeof(CmsKitHttpApiClientModule),
 | |
|     typeof(AbpAspNetCoreAuthenticationOAuthModule),
 | |
|     typeof(AbpAspNetCoreMvcClientModule),
 | |
|     typeof(AbpAspNetCoreMvcUiBasicThemeModule),
 | |
|     typeof(AbpAutofacModule),
 | |
|     typeof(AbpCachingStackExchangeRedisModule),
 | |
|     typeof(AbpHttpClientIdentityModelWebModule),
 | |
|     typeof(AbpIdentityWebModule),
 | |
|     typeof(AbpIdentityHttpApiClientModule),
 | |
|     typeof(AbpTenantManagementWebModule),
 | |
|     typeof(AbpTenantManagementHttpApiClientModule),
 | |
|     typeof(AbpFeatureManagementHttpApiClientModule),
 | |
|     typeof(AbpPermissionManagementHttpApiClientModule),
 | |
|     typeof(AbpAspNetCoreSerilogModule)
 | |
|     )]
 | |
| public class CmsKitWebHostModule : AbpModule
 | |
| {
 | |
|     public override void PreConfigureServices(ServiceConfigurationContext context)
 | |
|     {
 | |
|         FeatureConfigurer.Configure();
 | |
| 
 | |
|         context.Services.PreConfigure<AbpMvcDataAnnotationsLocalizationOptions>(options =>
 | |
|         {
 | |
|             options.AddAssemblyResource(
 | |
|                 typeof(CmsKitResource),
 | |
|                 typeof(CmsKitDomainSharedModule).Assembly,
 | |
|                 typeof(CmsKitApplicationContractsModule).Assembly,
 | |
|                 typeof(CmsKitWebHostModule).Assembly
 | |
|             );
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     public override void ConfigureServices(ServiceConfigurationContext context)
 | |
|     {
 | |
|         var hostingEnvironment = context.Services.GetHostingEnvironment();
 | |
|         var configuration = context.Services.GetConfiguration();
 | |
| 
 | |
|         ConfigureMenu(configuration);
 | |
|         ConfigureCache(configuration);
 | |
|         ConfigureUrls(configuration);
 | |
|         ConfigureAuthentication(context, configuration);
 | |
|         ConfigureAutoMapper();
 | |
|         ConfigureVirtualFileSystem(hostingEnvironment);
 | |
|         ConfigureSwaggerServices(context.Services);
 | |
|         ConfigureMultiTenancy();
 | |
|         ConfigureRedis(context, configuration, hostingEnvironment);
 | |
|     }
 | |
| 
 | |
|     private void ConfigureMenu(IConfiguration configuration)
 | |
|     {
 | |
|         Configure<AbpNavigationOptions>(options =>
 | |
|         {
 | |
|             options.MenuContributors.Add(new CmsKitWebHostMenuContributor(configuration));
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     private void ConfigureCache(IConfiguration configuration)
 | |
|     {
 | |
|         Configure<AbpDistributedCacheOptions>(options =>
 | |
|         {
 | |
|             options.KeyPrefix = "CmsKit:";
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     private void ConfigureUrls(IConfiguration configuration)
 | |
|     {
 | |
|         Configure<AppUrlOptions>(options =>
 | |
|         {
 | |
|             options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"];
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     private void ConfigureMultiTenancy()
 | |
|     {
 | |
|         Configure<AbpMultiTenancyOptions>(options =>
 | |
|         {
 | |
|             options.IsEnabled = MultiTenancyConsts.IsEnabled;
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
 | |
|     {
 | |
|         context.Services.AddAuthentication(options =>
 | |
|             {
 | |
|                 options.DefaultScheme = "Cookies";
 | |
|                 options.DefaultChallengeScheme = "oidc";
 | |
|             })
 | |
|             .AddCookie("Cookies", options =>
 | |
|             {
 | |
|                 options.ExpireTimeSpan = TimeSpan.FromDays(365);
 | |
|             })
 | |
|             .AddOpenIdConnect("oidc", options =>
 | |
|             {
 | |
|                 options.Authority = configuration["AuthServer:Authority"];
 | |
|                 options.RequireHttpsMetadata = false;
 | |
|                 options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
 | |
| 
 | |
|                 options.ClientId = configuration["AuthServer:ClientId"];
 | |
|                 options.ClientSecret = configuration["AuthServer:ClientSecret"];
 | |
| 
 | |
|                 options.SaveTokens = true;
 | |
|                 options.GetClaimsFromUserInfoEndpoint = true;
 | |
| 
 | |
|                 options.Scope.Add("role");
 | |
|                 options.Scope.Add("email");
 | |
|                 options.Scope.Add("phone");
 | |
|                 options.Scope.Add("CmsKit");
 | |
| 
 | |
|                 options.ClaimActions.MapJsonKey(AbpClaimTypes.UserName, "name");
 | |
|                 options.ClaimActions.DeleteClaim("name");
 | |
|             });
 | |
|     }
 | |
| 
 | |
|     private void ConfigureAutoMapper()
 | |
|     {
 | |
|         Configure<AbpAutoMapperOptions>(options =>
 | |
|         {
 | |
|             options.AddMaps<CmsKitWebHostModule>();
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     private void ConfigureVirtualFileSystem(IWebHostEnvironment hostingEnvironment)
 | |
|     {
 | |
|         if (hostingEnvironment.IsDevelopment())
 | |
|         {
 | |
|             Configure<AbpVirtualFileSystemOptions>(options =>
 | |
|             {
 | |
|                 options.FileSets.ReplaceEmbeddedByPhysical<CmsKitDomainSharedModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("..{0}..{0}src{0}Volo.CmsKit.Domain", Path.DirectorySeparatorChar)));
 | |
|                 options.FileSets.ReplaceEmbeddedByPhysical<CmsKitApplicationContractsModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("..{0}..{0}src{0}Volo.CmsKit.Application.Contracts", Path.DirectorySeparatorChar)));
 | |
|                 options.FileSets.ReplaceEmbeddedByPhysical<CmsKitWebModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("..{0}..{0}src{0}Volo.CmsKit.Web", Path.DirectorySeparatorChar)));
 | |
|             });
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private void ConfigureSwaggerServices(IServiceCollection services)
 | |
|     {
 | |
|         services.AddSwaggerGen(
 | |
|             options =>
 | |
|             {
 | |
|                 options.SwaggerDoc("v1", new OpenApiInfo { Title = "CmsKit API", Version = "v1" });
 | |
|                 options.DocInclusionPredicate((docName, description) => true);
 | |
|                 options.CustomSchemaIds(type => type.FullName);
 | |
|             }
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     private void ConfigureRedis(
 | |
|         ServiceConfigurationContext context,
 | |
|         IConfiguration configuration,
 | |
|         IWebHostEnvironment hostingEnvironment)
 | |
|     {
 | |
|         if (!hostingEnvironment.IsDevelopment())
 | |
|         {
 | |
|             var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
 | |
|             context.Services
 | |
|                 .AddDataProtection()
 | |
|                 .PersistKeysToStackExchangeRedis(redis, "CmsKit-Protection-Keys");
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public override void OnApplicationInitialization(ApplicationInitializationContext context)
 | |
|     {
 | |
|         var app = context.GetApplicationBuilder();
 | |
|         var env = context.GetEnvironment();
 | |
| 
 | |
|         if (env.IsDevelopment())
 | |
|         {
 | |
|             app.UseDeveloperExceptionPage();
 | |
|         }
 | |
|         else
 | |
|         {
 | |
|             app.UseErrorPage();
 | |
|             app.UseHsts();
 | |
|         }
 | |
| 
 | |
|         app.UseHttpsRedirection();
 | |
|         app.UseStaticFiles();
 | |
|         app.UseRouting();
 | |
|         app.UseAuthentication();
 | |
| 
 | |
|         if (MultiTenancyConsts.IsEnabled)
 | |
|         {
 | |
|             app.UseMultiTenancy();
 | |
|         }
 | |
| 
 | |
|         app.UseAbpRequestLocalization();
 | |
|         app.UseAuthorization();
 | |
| 
 | |
|         app.UseSwagger();
 | |
|         app.UseSwaggerUI(options =>
 | |
|         {
 | |
|             options.SwaggerEndpoint("/swagger/v1/swagger.json", "CmsKit API");
 | |
|         });
 | |
| 
 | |
|         app.UseAuditing();
 | |
|         app.UseAbpSerilogEnrichers();
 | |
|         app.UseConfiguredEndpoints();
 | |
|     }
 | |
| }
 |