The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Startup.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Builder;
  6. using Microsoft.AspNetCore.Identity;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.AspNetCore.HttpsPolicy;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.EntityFrameworkCore;
  12. using Teknik.Data;
  13. using Teknik.Utilities;
  14. using Microsoft.Extensions.Configuration;
  15. using Microsoft.Extensions.DependencyInjection;
  16. using Teknik.Logging;
  17. using System.IO;
  18. using Microsoft.Extensions.Logging;
  19. using Teknik.Configuration;
  20. using Teknik.Middleware;
  21. using Microsoft.AspNetCore.ResponseCompression;
  22. using System.IO.Compression;
  23. using System.Text;
  24. using Microsoft.AspNetCore.Authentication.Cookies;
  25. using IdentityServer4.Models;
  26. using Teknik.Areas.Accounts;
  27. using Microsoft.AspNetCore.Authentication.OpenIdConnect;
  28. using Teknik.Security;
  29. using Teknik.Attributes;
  30. using Teknik.Filters;
  31. namespace Teknik
  32. {
  33. public class Startup
  34. {
  35. public Startup(IConfiguration configuration, IHostingEnvironment env)
  36. {
  37. Configuration = configuration;
  38. Environment = env;
  39. }
  40. public IConfiguration Configuration { get; }
  41. public IHostingEnvironment Environment { get; }
  42. // This method gets called by the runtime. Use this method to add services to the container.
  43. public void ConfigureServices(IServiceCollection services)
  44. {
  45. string baseDir = Environment.ContentRootPath;
  46. string dataDir = Path.Combine(baseDir, "App_Data");
  47. AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
  48. // Setup IIS
  49. services.Configure<IISOptions>(options =>
  50. {
  51. options.ForwardClientCertificate = false;
  52. options.AutomaticAuthentication = false;
  53. });
  54. // HTTP Context
  55. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  56. // Create Configuration Singleton
  57. services.AddScoped<Config, Config>(opt => Config.Load(dataDir));
  58. // Add Tracking Filter scopes
  59. //services.AddScoped<TrackDownload>();
  60. //services.AddScoped<TrackLink>();
  61. //services.AddScoped<TrackPageView>();
  62. // Create the Database Context
  63. services.AddDbContext<TeknikEntities>(options => options
  64. .UseLazyLoadingProxies()
  65. .UseSqlServer(Configuration.GetConnectionString("TeknikEntities")), ServiceLifetime.Transient);
  66. // Cookie Policies
  67. services.Configure<CookiePolicyOptions>(options =>
  68. {
  69. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  70. options.CheckConsentNeeded = context => true;
  71. options.MinimumSameSitePolicy = SameSiteMode.None;
  72. });
  73. // Setup Authentication Service
  74. services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
  75. .AddCookie(options =>
  76. {
  77. options.Cookie.Domain = null;
  78. options.Cookie.Name = "TeknikAuthCore";
  79. options.LoginPath = "/User/User/Login";
  80. options.LogoutPath = "/User/User/Logout";
  81. options.EventsType = typeof(TeknikCookieAuthenticationEvents);
  82. });
  83. services.AddScoped<TeknikCookieAuthenticationEvents>();
  84. // Compression Response
  85. services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);
  86. services.AddResponseCompression(options => {
  87. options.Providers.Add<GzipCompressionProvider>();
  88. });
  89. // Sessions
  90. services.AddResponseCaching();
  91. services.AddMemoryCache();
  92. services.AddSession();
  93. // Core MVC
  94. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  95. //services.AddIdentityServer()
  96. // .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>();
  97. }
  98. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  99. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, TeknikEntities dbContext, Config config)
  100. {
  101. // Create and Migrate the database
  102. dbContext.Database.Migrate();
  103. // Initiate Logging
  104. loggerFactory.AddLogger(config);
  105. // Setup the HttpContext
  106. app.UseHttpContextSetup();
  107. // HttpContext Session
  108. app.UseSession(new SessionOptions()
  109. {
  110. IdleTimeout = TimeSpan.FromMinutes(30),
  111. Cookie = new CookieBuilder()
  112. {
  113. Domain = null,
  114. Name = "TeknikSession"
  115. }
  116. });
  117. // Use Exception Handling
  118. app.UseErrorHandler(config);
  119. if (env.IsDevelopment())
  120. {
  121. app.UseBrowserLink();
  122. //app.UseDeveloperExceptionPage();
  123. app.UseDatabaseErrorPage();
  124. }
  125. else
  126. {
  127. app.UseHsts();
  128. }
  129. // Performance Monitor the entire request
  130. app.UsePerformanceMonitor();
  131. // Custom Middleware
  132. app.UseBlacklist();
  133. app.UseCORS();
  134. app.UseCSP();
  135. // Cache Responses
  136. app.UseResponseCaching();
  137. app.UseHttpsRedirection();
  138. app.UseStaticFiles();
  139. app.UseCookiePolicy();
  140. app.UseAuthentication();
  141. app.UseMvc(routes =>
  142. {
  143. routes.BuildRoutes(config);
  144. });
  145. }
  146. }
  147. }