Startup.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Bo Repos
  55. services.AddScoped<DataDomain, BOContext>();
  56. services.AddScoped<IDomainContextResolver<DataDomain>, DomainContextResolver<DataDomain>>();
  57. services.AddScoped<IUnitOfWork<DataDomain>, UnitOfWorkDataDomain>();
  58. services.AddScoped<UserRepo>();
  59. services.AddScoped<WorkspaceRepo>();
  60. services.AddScoped<BillingPlanRepo>();
  61. services.AddScoped<MimeTypeRepo>();
  62. services.AddScoped<StatisticRepo>();
  63. services.AddScoped<WorkspaceRepo>();
  64. // Session framework
  65. services.AddDistributedMemoryCache();
  66. services.AddSession(options =>
  67. {
  68. options.IdleTimeout = TimeSpan.Parse(cfg.System.SessionIdleTimeout);
  69. });
  70. services.AddControllersWithViews(options =>
  71. {
  72. var policy = new AuthorizationPolicyBuilder()
  73. .RequireAuthenticatedUser()
  74. .Build();
  75. options.Filters.Add(new AuthorizeFilter(policy));
  76. });
  77. services.AddRazorPages();
  78. }
  79. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  80. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, IServiceProvider service)
  81. {
  82. if (env.IsDevelopment())
  83. {
  84. app.UseDeveloperExceptionPage();
  85. }
  86. else
  87. {
  88. app.UseExceptionHandler("/Home/Error");
  89. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  90. app.UseHsts();
  91. }
  92. //May create IF database structure
  93. using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
  94. {
  95. var context = serviceScope.ServiceProvider.GetRequiredService<IFDbContext>();
  96. context.Database.EnsureCreated();
  97. }
  98. _srvConfiguration = (ConfigurationService)service.GetService(typeof(ConfigurationService));
  99. lifetime.ApplicationStarted.Register(OnApplicationStarted);
  100. app.UseHttpsRedirection();
  101. app.UseStaticFiles();
  102. app.UseRouting();
  103. // Security support
  104. app.UseAuthentication();
  105. app.UseAuthorization();
  106. // Session support
  107. app.UseSession();
  108. app.UseEndpoints(endpoints =>
  109. {
  110. endpoints.MapControllerRoute(
  111. name: "default",
  112. pattern: "{controller=Home}/{action=Index}/{id?}");
  113. endpoints.MapRazorPages();
  114. });
  115. }
  116. private void OnApplicationStarted()
  117. {
  118. _srvConfiguration.Install();
  119. }
  120. }
  121. }