Startup.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Authentication.Cookies;
  7. using Microsoft.AspNetCore.Builder;
  8. using Microsoft.AspNetCore.Hosting;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.AspNetCore.HttpsPolicy;
  11. using Microsoft.AspNetCore.Rewrite;
  12. using Microsoft.Extensions.Configuration;
  13. using Microsoft.Extensions.DependencyInjection;
  14. using Microsoft.Extensions.FileProviders;
  15. using Microsoft.Extensions.Hosting;
  16. using SuperAdmin.Controllers;
  17. using SuperAdmin.Extensions;
  18. using SuperCms.Repositories;
  19. namespace SuperAdmin
  20. {
  21. public class Startup
  22. {
  23. public Startup(IConfiguration configuration)
  24. {
  25. Configuration = configuration;
  26. }
  27. public IConfiguration Configuration { get; }
  28. // This method gets called by the runtime. Use this method to add services to the container.
  29. public void ConfigureServices(IServiceCollection services)
  30. {
  31. //services.AddControllersWithViews();
  32. services.AddControllersWithViews().AddRazorRuntimeCompilation();
  33. services.AddTransient<ISuperAdminRepo, SuperAdminRepo>();
  34. services.AddSingleton<IConfiguration>(Configuration);
  35. services.AddMvc();
  36. //.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
  37. //.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
  38. services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
  39. services.AddDistributedMemoryCache(); // Adds a default in-memory implementation of IDistributedCache
  40. services.AddSession(options =>
  41. {
  42. options.IdleTimeout = TimeSpan.FromSeconds(600);
  43. options.Cookie.HttpOnly = true;
  44. options.Cookie.IsEssential = true;
  45. });
  46. //services.AddSession();
  47. services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
  48. services.AddHttpContextAccessor();
  49. services.AddMvc(options =>
  50. {
  51. options.Filters.Add(new ConfigAction(
  52. Configuration.GetSection("MyConfig")
  53. ));
  54. });
  55. }
  56. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  57. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  58. {
  59. if (env.IsDevelopment())
  60. {
  61. app.UseDeveloperExceptionPage();
  62. }
  63. else
  64. {
  65. app.UseExceptionHandler("/Home/Error");
  66. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  67. app.UseHsts();
  68. }
  69. app.UseSession();
  70. app.UseCookiePolicy();
  71. app.UseHttpsRedirection();
  72. app.UseStaticFiles();
  73. string pathContent = Configuration.GetSection(UtilsController.Constant.PATH_CONTENT).Value;
  74. if (pathContent != null && pathContent.Length > 0)
  75. {
  76. app.UseStaticFiles(new StaticFileOptions()
  77. {
  78. FileProvider = new PhysicalFileProvider(Configuration.GetSection(UtilsController.Constant.PATH_OUTSIDE).Value),
  79. RequestPath = new PathString(pathContent)
  80. });
  81. }
  82. app.UseRouting();
  83. app.UseAuthorization();
  84. String domain = Configuration.GetSection(UtilsController.Constant.SUB_DOMAIN).Value;
  85. app.UsePathBase(domain == null || domain == "" ? "/" : domain);
  86. app.UseEndpoints(endpoints =>
  87. {
  88. endpoints.MapControllerRoute(
  89. name: "default",
  90. pattern: domain + "/{controller=Home}/{action=Index}/{id?}");
  91. });
  92. }
  93. }
  94. }