| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using System;
- 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;
- 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<BOContext>();
- services.AddDbContext<IFDbContext>();
- // Identity framework enable
- services.AddIdentity<IdentityUser, IdentityRole>()
- .AddEntityFrameworkStores<IFDbContext>()
- .AddDefaultTokenProviders();
- // Inject IPrincipal to get current user in all levels
- services.AddHttpContextAccessor();
- services.AddTransient<IPrincipal>(provider => provider.GetService<IHttpContextAccessor>().HttpContext?.User);
- // Bo Services
- var cfg = new Configuration();
- Configuration.GetSection("Bo").Bind(cfg);
- services.Configure<Configuration>(Configuration.GetSection("Bo"));
- services.AddScoped<AccessService>();
- services.AddSingleton<ConfigurationService>();
- services.AddScoped<UserService>();
- services.AddScoped<StatisticsService>();
- services.AddScoped<CatalogueService>();
- services.AddScoped<WorkspaceService>();
- // Bo Repos
- services.AddScoped<DataDomain, BOContext>();
- services.AddScoped<IDomainContextResolver<DataDomain>, DomainContextResolver<DataDomain>>();
- services.AddScoped<IUnitOfWork<DataDomain>, UnitOfWorkDataDomain>();
- services.AddScoped<UserRepo>();
- services.AddScoped<WorkspaceRepo>();
- services.AddScoped<BillingPlanRepo>();
- services.AddScoped<MimeTypeRepo>();
- services.AddScoped<StatisticRepo>();
- services.AddScoped<WorkspaceRepo>();
- // 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)
- {
- 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<IServiceScopeFactory>().CreateScope())
- {
- var context = serviceScope.ServiceProvider.GetRequiredService<IFDbContext>();
- 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();
- }
- }
- }
|