177 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			177 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using KonSoft.Admin;
 | |
| using KonSoft.Admin.EntityFrameworkCore;
 | |
| using KonSoft.Shared.Hosting.AspNetCore;
 | |
| using KonSoft.Shared.Hosting.Microservices;
 | |
| using KonSoft.Shared.Localization.Localization;
 | |
| using Localization.Resources.AbpUi;
 | |
| using Microsoft.AspNetCore.Builder;
 | |
| using Microsoft.AspNetCore.Cors;
 | |
| using Microsoft.AspNetCore.HttpOverrides;
 | |
| using Microsoft.Extensions.DependencyInjection;
 | |
| using Microsoft.Extensions.Hosting;
 | |
| using System;
 | |
| using System.Linq;
 | |
| using System.Net;
 | |
| using Volo.Abp;
 | |
| using Volo.Abp.Account;
 | |
| using Volo.Abp.Account.Localization;
 | |
| using Volo.Abp.Account.Web;
 | |
| using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
 | |
| using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic;
 | |
| using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic.Bundling;
 | |
| using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
 | |
| using Volo.Abp.Auditing;
 | |
| using Volo.Abp.BackgroundJobs;
 | |
| using Volo.Abp.Localization;
 | |
| using Volo.Abp.Modularity;
 | |
| using Volo.Abp.OpenIddict;
 | |
| using Volo.Abp.Security.Claims;
 | |
| using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
 | |
| 
 | |
| namespace KonSoft;
 | |
| 
 | |
| [DependsOn(
 | |
|     typeof(AbpAccountWebOpenIddictModule),
 | |
|     typeof(AbpAccountApplicationModule),
 | |
|     typeof(AbpAccountHttpApiModule), 
 | |
|     typeof(AdminApplicationModule),
 | |
|     typeof(AdminEntityFrameworkCoreModule),
 | |
|     typeof(AbpAspNetCoreMvcUiBasicThemeModule),
 | |
|     typeof(KonSoftSharedHostingMicroservicesModule)
 | |
| )]
 | |
| public class KonSoftAuthServerModule : AbpModule
 | |
| {
 | |
|     public override void PreConfigureServices(ServiceConfigurationContext context)
 | |
|     {
 | |
|         var hostingEnvironment = context.Services.GetHostingEnvironment();
 | |
|         var configuration = context.Services.GetConfiguration();
 | |
| 
 | |
|         PreConfigure<OpenIddictBuilder>(builder =>
 | |
|         {
 | |
|             builder.AddValidation(options =>
 | |
|             {
 | |
|                 options.AddAudiences("KonSoft");
 | |
|                 options.UseLocalServer();
 | |
|                 options.UseAspNetCore();
 | |
|             });
 | |
|         });
 | |
| 
 | |
|         context.Services.Configure<ForwardedHeadersOptions>(options =>
 | |
|         {
 | |
|             options.KnownNetworks.Clear();
 | |
|             options.KnownProxies.Clear();
 | |
|             options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
 | |
|         });
 | |
| 
 | |
|         if (!hostingEnvironment.IsDevelopment())
 | |
|         {
 | |
|             PreConfigure<AbpOpenIddictAspNetCoreOptions>(options =>
 | |
|             {
 | |
|                 options.AddDevelopmentEncryptionAndSigningCertificate = false;
 | |
|             });
 | |
| 
 | |
|             PreConfigure<OpenIddictServerBuilder>(serverBuilder =>
 | |
|             {
 | |
|                 serverBuilder.AddProductionEncryptionAndSigningCertificate("openiddict.pfx",
 | |
|                     "59464dba-b66e-48cd-8b81-2e4a9c08c977");
 | |
|             });
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public override void ConfigureServices(ServiceConfigurationContext context)
 | |
|     {
 | |
|         var hostingEnvironment = context.Services.GetHostingEnvironment();
 | |
|         var configuration = context.Services.GetConfiguration();
 | |
| 
 | |
|         Configure<AbpLocalizationOptions>(options =>
 | |
|         {
 | |
|             options.Resources
 | |
|                 .Get<KonSoftResource>()
 | |
|                 .AddBaseTypes(
 | |
|                     typeof(AbpUiResource),
 | |
|                     typeof(AccountResource)
 | |
|                 );
 | |
|         });
 | |
| 
 | |
|         Configure<AbpBundlingOptions>(options =>
 | |
|         {
 | |
|             options.StyleBundles.Configure(
 | |
|                 BasicThemeBundles.Styles.Global,
 | |
|                 bundle => { bundle.AddFiles("/global-styles.css"); }
 | |
|             );
 | |
|         });
 | |
| 
 | |
|         Configure<AbpAuditingOptions>(options =>
 | |
|         {
 | |
|             options.IsEnabledForGetRequests = true;
 | |
|             options.ApplicationName = "AuthServer";
 | |
|         });
 | |
| 
 | |
|         Configure<AbpBackgroundJobOptions>(options => { options.IsJobExecutionEnabled = false; });
 | |
| 
 | |
|         context.Services.Configure<AbpClaimsPrincipalFactoryOptions>(options =>
 | |
|         {
 | |
|             options.IsDynamicClaimsEnabled = true;
 | |
|         });
 | |
| 
 | |
|         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();
 | |
|             });
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     public override void OnApplicationInitialization(ApplicationInitializationContext context)
 | |
|     {
 | |
|         var app = context.GetApplicationBuilder();
 | |
|         var env = context.GetEnvironment();
 | |
| 
 | |
|         if (!env.IsDevelopment())
 | |
|         {
 | |
|             app.UseErrorPage();
 | |
|             app.UseForwardedHeaders();
 | |
|             app.UseHsts();
 | |
|         }
 | |
| 
 | |
|         if (env.IsDevelopment())
 | |
|         {
 | |
|             app.UseDeveloperExceptionPage();
 | |
|             app.UseForwardedHeaders();
 | |
|         }
 | |
| 
 | |
|         app.UseAbpRequestLocalization();
 | |
| 
 | |
|         app.UseCorrelationId();
 | |
|         app.UseStaticFiles();
 | |
|         app.UseRouting();
 | |
|         app.UseCors();
 | |
|         app.UseAuthentication();
 | |
|         app.UseAbpOpenIddictValidation();
 | |
| 
 | |
|         if (KonSoftConsts.MultiTenancyEnabled)
 | |
|         {
 | |
|             app.UseMultiTenancy();
 | |
|         }
 | |
| 
 | |
|         app.UseUnitOfWork();
 | |
|         app.UseDynamicClaims();
 | |
|         app.UseAuthorization();
 | |
| 
 | |
|         app.UseAuditing();
 | |
|         app.UseAbpSerilogEnrichers();
 | |
|         app.UseConfiguredEndpoints();
 | |
|     }
 | |
| } |