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.
		
		
		
		
		
			
		
			
				
					
					
						
							149 lines
						
					
					
						
							5.5 KiB
						
					
					
				
			
		
		
	
	
							149 lines
						
					
					
						
							5.5 KiB
						
					
					
				| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.IO;
 | |
| using System.Linq;
 | |
| using System.Security.Claims;
 | |
| using System.Threading.Tasks;
 | |
| using Microsoft.AspNetCore.Builder;
 | |
| using Microsoft.AspNetCore.DataProtection;
 | |
| using Microsoft.Extensions.DependencyInjection;
 | |
| using StackExchange.Redis;
 | |
| using Microsoft.OpenApi.Models;
 | |
| using Swashbuckle.AspNetCore.Swagger;
 | |
| using Volo.Abp;
 | |
| using Volo.Abp.Auditing;
 | |
| using Volo.Abp.AuditLogging.EntityFrameworkCore;
 | |
| using Volo.Abp.Autofac;
 | |
| using Volo.Abp.Data;
 | |
| using Volo.Abp.EntityFrameworkCore;
 | |
| using Volo.Abp.EntityFrameworkCore.SqlServer;
 | |
| using Volo.Abp.EventBus.RabbitMq;
 | |
| using Volo.Abp.Guids;
 | |
| using Volo.Abp.Http.Client.IdentityModel;
 | |
| using Volo.Abp.Identity;
 | |
| using Volo.Abp.Localization;
 | |
| using Volo.Abp.Modularity;
 | |
| using Volo.Abp.PermissionManagement.EntityFrameworkCore;
 | |
| using Volo.Abp.Security.Claims;
 | |
| using Volo.Abp.SettingManagement.EntityFrameworkCore;
 | |
| using Volo.Abp.Threading;
 | |
| using Volo.Blogging;
 | |
| using Volo.Blogging.Blogs;
 | |
| using Volo.Blogging.Files;
 | |
| using Volo.Blogging.MongoDB;
 | |
| 
 | |
| namespace BloggingService.Host
 | |
| {
 | |
|     [DependsOn(
 | |
|         typeof(AbpAutofacModule),
 | |
|         typeof(AbpEventBusRabbitMqModule),
 | |
|         typeof(AbpEntityFrameworkCoreSqlServerModule),
 | |
|         typeof(AbpAuditLoggingEntityFrameworkCoreModule),
 | |
|         typeof(AbpPermissionManagementEntityFrameworkCoreModule),
 | |
|         typeof(AbpSettingManagementEntityFrameworkCoreModule),
 | |
|         typeof(BloggingHttpApiModule),
 | |
|         typeof(BloggingMongoDbModule),
 | |
|         typeof(BloggingApplicationModule),
 | |
|         typeof(AbpHttpClientIdentityModelModule),
 | |
|         typeof(AbpIdentityHttpApiClientModule)
 | |
|         )]
 | |
|     public class BloggingServiceHostModule : AbpModule
 | |
|     {
 | |
|         public override void ConfigureServices(ServiceConfigurationContext context)
 | |
|         {
 | |
|             var configuration = context.Services.GetConfiguration();
 | |
|             var hostingEnvironment = context.Services.GetHostingEnvironment();
 | |
| 
 | |
|             context.Services.AddAuthentication("Bearer")
 | |
|                 .AddIdentityServerAuthentication(options =>
 | |
|                 {
 | |
|                     options.Authority = configuration["AuthServer:Authority"];
 | |
|                     options.ApiName = configuration["AuthServer:ApiName"];
 | |
|                     options.RequireHttpsMetadata = false;
 | |
|                 });
 | |
| 
 | |
|             context.Services.AddSwaggerGen(options =>
 | |
|             {
 | |
|                 options.SwaggerDoc("v1", new OpenApiInfo {Title = "Blogging Service API", Version = "v1"});
 | |
|                 options.DocInclusionPredicate((docName, description) => true);
 | |
|                 options.CustomSchemaIds(type => type.FullName);
 | |
|             });
 | |
| 
 | |
|             Configure<AbpLocalizationOptions>(options =>
 | |
|             {
 | |
|                 options.Languages.Add(new LanguageInfo("en", "en", "English"));
 | |
|             });
 | |
|             
 | |
|             Configure<AbpDbContextOptions>(options =>
 | |
|             {
 | |
|                 options.UseSqlServer();
 | |
|             });
 | |
| 
 | |
|             Configure<BlogFileOptions>(options =>
 | |
|             {
 | |
|                 options.FileUploadLocalFolder = Path.Combine(hostingEnvironment.WebRootPath, "files");
 | |
|             });
 | |
| 
 | |
|             context.Services.AddStackExchangeRedisCache(options =>
 | |
|             {
 | |
|                 options.Configuration = configuration["Redis:Configuration"];
 | |
|             });
 | |
| 
 | |
|             Configure<AbpAuditingOptions>(options =>
 | |
|             {
 | |
|                 options.IsEnabledForGetRequests = true;
 | |
|                 options.ApplicationName = "BloggingService";
 | |
|             });
 | |
| 
 | |
|             var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
 | |
|             context.Services.AddDataProtection()
 | |
|                 .PersistKeysToStackExchangeRedis(redis, "MsDemo-DataProtection-Keys");
 | |
|         }
 | |
| 
 | |
|         public override void OnApplicationInitialization(ApplicationInitializationContext context)
 | |
|         {
 | |
|             var app = context.GetApplicationBuilder();
 | |
| 
 | |
|             app.UseCorrelationId();
 | |
|             app.UseVirtualFiles();
 | |
|             app.UseRouting();
 | |
|             app.UseAuthentication();
 | |
| 
 | |
|             app.Use(async (ctx, next) =>
 | |
|             {
 | |
|                 var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService<ICurrentPrincipalAccessor>();
 | |
|                 var map = new Dictionary<string, string>()
 | |
|                 {
 | |
|                     { "sub", AbpClaimTypes.UserId },
 | |
|                     { "role", AbpClaimTypes.Role },
 | |
|                     { "email", AbpClaimTypes.Email },
 | |
|                     //any other map
 | |
|                 };
 | |
|                 var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.Keys.Contains(p.Type)).ToList();
 | |
|                 currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer))));
 | |
|                 await next();
 | |
|             });
 | |
| 
 | |
|             app.UseAbpRequestLocalization(); //TODO: localization?
 | |
|             app.UseSwagger();
 | |
|             app.UseSwaggerUI(options =>
 | |
|             {
 | |
|                 options.SwaggerEndpoint("/swagger/v1/swagger.json", "Blogging Service API");
 | |
|             });
 | |
|             app.UseAuditing();
 | |
|             app.UseMvcWithDefaultRouteAndArea();
 | |
| 
 | |
|             //TODO: Problem on a clustered environment
 | |
|             AsyncHelper.RunSync(async () =>
 | |
|             {
 | |
|                 using (var scope = context.ServiceProvider.CreateScope())
 | |
|                 {
 | |
|                     await scope.ServiceProvider
 | |
|                         .GetRequiredService<IDataSeeder>()
 | |
|                         .SeedAsync();
 | |
|                 }
 | |
|             });
 | |
|         }
 | |
|     }
 | |
| }
 |