Startup.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Security.Principal;
  3. using BO.AppServer.Business.Configuration;
  4. using BO.AppServer.Business.Services;
  5. using Microsoft.AspNetCore.Authorization;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.AspNetCore.Mvc.Authorization;
  9. using Microsoft.Extensions.Configuration;
  10. using Microsoft.Extensions.DependencyInjection;
  11. using Microsoft.Extensions.Hosting;
  12. using BO.AppServer.Data.Entity;
  13. using BO.AppServer.Data.Repository;
  14. using BO.AppServer.Web.Data;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Identity;
  17. using Quadarax.Foundation.Core.Data.Domain;
  18. using Quadarax.Foundation.Core.Data.Interface.Domain;
  19. namespace AppServer
  20. {
  21. public class Startup
  22. {
  23. private ConfigurationService _srvConfiguration;
  24. public Startup(IConfiguration configuration)
  25. {
  26. Configuration = configuration;
  27. }
  28. public IConfiguration Configuration { get; }
  29. // This method gets called by the runtime. Use this method to add services to the container.
  30. public void ConfigureServices(IServiceCollection services)
  31. {
  32. //services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
  33. // .AddAzureAD(options => Configuration.Bind("AzureAd", options));
  34. // EF enable
  35. services.AddDbContext<BOContext>();
  36. services.AddDbContext<IFDbContext>();
  37. // Identity framework enable
  38. services.AddIdentity<IdentityUser, IdentityRole>()
  39. .AddEntityFrameworkStores<IFDbContext>()
  40. .AddDefaultTokenProviders();
  41. // Inject IPrincipal to get current user in all levels
  42. services.AddHttpContextAccessor();
  43. services.AddTransient<IPrincipal>(provider => provider.GetService<IHttpContextAccessor>().HttpContext?.User);
  44. // Bo Services
  45. var cfg = new Configuration();
  46. Configuration.GetSection("Bo").Bind(cfg);
  47. services.Configure<Configuration>(Configuration.GetSection("Bo"));
  48. services.AddScoped<AccessService>();
  49. services.AddSingleton<ConfigurationService>();
  50. services.AddScoped<UserService>();
  51. services.AddScoped<StatisticsService>();
  52. services.AddScoped<CatalogueService>();
  53. services.AddScoped<WorkspaceService>();
  54. services.AddScoped<StorageService>();
  55. // Bo Repos
  56. services.AddScoped<DataDomain, BOContext>();
  57. services.AddScoped<IDomainContextResolver<DataDomain>, DomainContextResolver<DataDomain>>();
  58. services.AddScoped<IUnitOfWork<DataDomain>, UnitOfWorkDataDomain>();
  59. services.AddScoped<UserRepo>();
  60. services.AddScoped<WorkspaceRepo>();
  61. services.AddScoped<BillingPlanRepo>();
  62. services.AddScoped<MimeTypeRepo>();
  63. services.AddScoped<StatisticRepo>();
  64. services.AddScoped<WorkspaceRepo>();
  65. services.AddScoped<MetadocumentRepo>();
  66. services.AddScoped<ArtifactRepo>();
  67. // Session framework
  68. services.AddDistributedMemoryCache();
  69. services.AddSession(options =>
  70. {
  71. options.IdleTimeout = TimeSpan.Parse(cfg.System.SessionIdleTimeout);
  72. });
  73. services.AddControllersWithViews(options =>
  74. {
  75. var policy = new AuthorizationPolicyBuilder()
  76. .RequireAuthenticatedUser()
  77. .Build();
  78. options.Filters.Add(new AuthorizeFilter(policy));
  79. });
  80. services.AddRazorPages();
  81. }
  82. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  83. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, IServiceProvider service)
  84. {
  85. if (env.IsDevelopment())
  86. {
  87. app.UseDeveloperExceptionPage();
  88. }
  89. else
  90. {
  91. app.UseExceptionHandler("/Home/Error");
  92. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  93. app.UseHsts();
  94. }
  95. //May create IF database structure
  96. using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
  97. {
  98. var context = serviceScope.ServiceProvider.GetRequiredService<IFDbContext>();
  99. context.Database.EnsureCreated();
  100. }
  101. _srvConfiguration = (ConfigurationService)service.GetService(typeof(ConfigurationService));
  102. lifetime.ApplicationStarted.Register(OnApplicationStarted);
  103. app.UseHttpsRedirection();
  104. app.UseStaticFiles();
  105. app.UseRouting();
  106. // Security support
  107. app.UseAuthentication();
  108. app.UseAuthorization();
  109. // Session support
  110. app.UseSession();
  111. app.UseEndpoints(endpoints =>
  112. {
  113. endpoints.MapControllerRoute(
  114. name: "default",
  115. pattern: "{controller=Home}/{action=Index}/{id?}");
  116. endpoints.MapRazorPages();
  117. });
  118. }
  119. private void OnApplicationStarted()
  120. {
  121. _srvConfiguration.Install();
  122. }
  123. }
  124. }