Startup.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using DutchTreat.Data;
  2. using DutchTreat.Data.Entities;
  3. using DutchTreat.Services;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Identity;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.Extensions.Hosting;
  11. using Microsoft.IdentityModel.Tokens;
  12. using Newtonsoft.Json;
  13. using System.Reflection;
  14. using System.Text;
  15. namespace DutchTreat
  16. {
  17. public class Startup
  18. {
  19. private readonly IConfiguration _config;
  20. public Startup(IConfiguration config)
  21. {
  22. _config = config;
  23. }
  24. // This method gets called by the runtime. Use this method to add services to the container.
  25. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
  26. public void ConfigureServices(IServiceCollection services)
  27. {
  28. services.AddIdentity<StoreUser, IdentityRole>(cfg =>
  29. {
  30. cfg.User.RequireUniqueEmail = true;
  31. }).AddEntityFrameworkStores<DutchContext>();
  32. services.AddAuthentication()
  33. .AddCookie()
  34. .AddJwtBearer(cfg =>
  35. {
  36. cfg.TokenValidationParameters = new TokenValidationParameters()
  37. {
  38. ValidIssuer = _config["Token:Issuer"],
  39. ValidAudience = _config["Token:Audience"],
  40. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Token:Key"]))
  41. };
  42. });
  43. services.AddDbContext<DutchContext>(cfg =>
  44. {
  45. cfg.UseSqlServer(_config.GetConnectionString("DutchConnectionString"));
  46. });
  47. services.AddAutoMapper(Assembly.GetExecutingAssembly());
  48. services.AddTransient<IMailService, NullMailService>();
  49. services.AddTransient<DutchSeeder>();
  50. services.AddScoped<IDutchRepository, DutchRepository>();
  51. services.AddMvc()
  52. .AddRazorRuntimeCompilation()
  53. .AddNewtonsoftJson(cfg => cfg.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
  54. }
  55. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  56. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  57. {
  58. if (env.IsEnvironment("Development"))
  59. {
  60. app.UseDeveloperExceptionPage(); // --> For debug
  61. }
  62. else
  63. {
  64. app.UseExceptionHandler("/error");
  65. }
  66. app.UseStaticFiles();
  67. //app.UseDefaultFiles();
  68. //Routing MVC
  69. app.UseRouting();
  70. app.UseAuthentication();
  71. app.UseAuthorization();
  72. app.UseEndpoints(cfg => {
  73. cfg.MapRazorPages();
  74. cfg.MapControllerRoute("Default",
  75. "/{controller}/{action}/{id?}",
  76. new { controller = "App", action = "Index" });
  77. });
  78. }
  79. }
  80. }