using System; using System.IO.Abstractions; using System.Security.Principal; using BO.AppServer.Business.Configuration; using BO.AppServer.Business.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using BO.AppServer.Data.Entity; using BO.AppServer.Data.Repository; using BO.AppServer.Web.Data; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Quadarax.Foundation.Core.Data.Domain; using Quadarax.Foundation.Core.Data.Interface.Domain; namespace AppServer { public class Startup { private ConfigurationService _srvConfiguration; private IWebHostEnvironment _env; public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //services.AddAuthentication(AzureADDefaults.AuthenticationScheme) // .AddAzureAD(options => Configuration.Bind("AzureAd", options)); // EF enable services.AddDbContext(); services.AddDbContext(); // Identity framework enable services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); //Use file abstractions services.AddSingleton(); // Inject IPrincipal to get current user in all levels services.AddHttpContextAccessor(); services.AddTransient(provider => provider.GetService().HttpContext?.User); // Bo Services var cfg = new Configuration(); Configuration.GetSection("Bo").Bind(cfg); services.Configure(Configuration.GetSection("Bo")); services.AddScoped(); services.AddSingleton(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // Bo Repos services.AddScoped(); services.AddScoped, DomainContextResolver>(); services.AddScoped, UnitOfWorkDataDomain>(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // Session framework services.AddDistributedMemoryCache(); services.AddSession(options => { options.IdleTimeout = TimeSpan.Parse(cfg.System.SessionIdleTimeout); }); services.AddControllersWithViews(options => { var policy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); options.Filters.Add(new AuthorizeFilter(policy)); }); services.AddRazorPages(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, IServiceProvider service) { _env = env; if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } //May create IF database structure using (var serviceScope = app.ApplicationServices.GetService().CreateScope()) { var context = serviceScope.ServiceProvider.GetRequiredService(); context.Database.EnsureCreated(); } _srvConfiguration = (ConfigurationService)service.GetService(typeof(ConfigurationService)); lifetime.ApplicationStarted.Register(OnApplicationStarted); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); // Security support app.UseAuthentication(); app.UseAuthorization(); // Session support app.UseSession(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapRazorPages(); }); } private void OnApplicationStarted() { _srvConfiguration.Install(); if (_env.IsDevelopment()) { _srvConfiguration.InstallDev(); } } } }