Startup.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Microsoft.AspNetCore.Authentication.Cookies;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.HttpsPolicy;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.FileProviders;
  9. using Microsoft.Extensions.Hosting;
  10. using Microsoft.Extensions.Options;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.Linq;
  15. using System.Threading.Tasks;
  16. namespace Pulsa
  17. {
  18. public class Startup
  19. {
  20. public Startup(IConfiguration configuration)
  21. {
  22. Configuration = configuration;
  23. }
  24. public IConfiguration Configuration { get; }
  25. // This method gets called by the runtime. Use this method to add services to the container.
  26. public void ConfigureServices(IServiceCollection services)
  27. {
  28. services.AddControllersWithViews().AddRazorRuntimeCompilation();
  29. services.AddSingleton<IConfiguration>(Configuration);
  30. services.AddLocalization(options => options.ResourcesPath = "Resources");
  31. services.AddMvc()
  32. .AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
  33. .AddDataAnnotationsLocalization();
  34. services.Configure<RequestLocalizationOptions>(options =>
  35. {
  36. var cultures = new List<CultureInfo> {
  37. new CultureInfo("en"),
  38. new CultureInfo("te"),
  39. new CultureInfo("fr")
  40. };
  41. options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("fr");
  42. options.SupportedCultures = cultures;
  43. options.SupportedUICultures = cultures;
  44. });
  45. services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
  46. services.AddDistributedMemoryCache(); // Adds a default in-memory implementation of IDistributedCache
  47. services.AddSession(options =>
  48. {
  49. options.IdleTimeout = TimeSpan.FromSeconds(600);
  50. options.Cookie.HttpOnly = true;
  51. options.Cookie.IsEssential = true;
  52. //options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
  53. });
  54. //services.AddSession();
  55. services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
  56. services.AddHttpContextAccessor();
  57. }
  58. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  59. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  60. {
  61. if (env.IsDevelopment())
  62. {
  63. app.UseDeveloperExceptionPage();
  64. }
  65. else
  66. {
  67. app.UseExceptionHandler("/Home/Error");
  68. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  69. app.UseHsts();
  70. }
  71. app.UseSession();
  72. app.UseHttpsRedirection();
  73. app.UseStaticFiles();
  74. app.UseRequestLocalization(app.ApplicationServices.GetRequiredService<IOptions<RequestLocalizationOptions>>().Value);
  75. string pathContent = Configuration.GetSection("pathContent").Value;
  76. if (pathContent != null && pathContent.Length > 0)
  77. {
  78. app.UseStaticFiles(new StaticFileOptions()
  79. {
  80. FileProvider = new PhysicalFileProvider(Configuration.GetSection("pathOutside").Value),
  81. RequestPath = new PathString(pathContent)
  82. });
  83. }
  84. app.UseRouting();
  85. app.UseAuthorization();
  86. app.UseEndpoints(endpoints =>
  87. {
  88. endpoints.MapControllerRoute(
  89. name: "default",
  90. pattern: "{controller=Home}/{action=Index}/{id?}");
  91. });
  92. }
  93. }
  94. }