source: KernelRecordsMVC.Web/Program.cs@ 1dcdb2c

main
Last change on this file since 1dcdb2c was 1dcdb2c, checked in by mmilevski <markomilevski3@…>, 3 weeks ago

Changed controllers to include transactions, Implemented password hashing.

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[08aefc6]1using Microsoft.EntityFrameworkCore;
[fa0fbaf]2using Npgsql;
3using Npgsql.NameTranslation;
4using KernelRecordsMVC.Domain.Enums;
5using KernelRecordsMVC.Infrastructure.Data;
[1dcdb2c]6using Microsoft.AspNetCore.Identity;
7using KernelRecordsMVC.Models;
[08aefc6]8
9var builder = WebApplication.CreateBuilder(args);
10
[fa0fbaf]11// ==========================================
12// AUTHENTICATION
13// ==========================================
14
15builder.Services
16 .AddAuthentication("Cookies")
17 .AddCookie("Cookies", options =>
18 {
19 options.LoginPath = "/Account/Login";
20 options.AccessDeniedPath = "/Account/Login";
21 options.ExpireTimeSpan = TimeSpan.FromHours(8);
22 options.SlidingExpiration = true;
23 });
24
25builder.Services.AddAuthorization();
26
[08aefc6]27builder.Services.AddControllersWithViews();
28
[1dcdb2c]29builder.Services.AddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
30
[fa0fbaf]31// ==========================================
32// DATABASE
33// ==========================================
34
35var dataSourceBuilder = new NpgsqlDataSourceBuilder(
36 builder.Configuration.GetConnectionString("KernelRecords"));
37
38var nameTranslator = new NpgsqlNullNameTranslator();
39
40dataSourceBuilder.MapEnum<AdminType>(
41 "project.admin_type",
42 nameTranslator);
43
44dataSourceBuilder.MapEnum<ProductFormat>(
45 "project.product_format",
46 nameTranslator);
47
48dataSourceBuilder.MapEnum<PaymentMethodType>(
49 "project.payment_method_type",
50 nameTranslator);
51
52dataSourceBuilder.MapEnum<OrderStatusType>(
53 "project.order_status_type",
54 nameTranslator);
55
56dataSourceBuilder.MapEnum<ModificationType>(
57 "project.modification_type",
58 nameTranslator);
59
60dataSourceBuilder.MapEnum<ArtistReleaseType>(
61 "project.artist_release_type",
62 nameTranslator);
63
64var dataSource = dataSourceBuilder.Build();
65
[08aefc6]66builder.Services.AddDbContext<KernelRecordsContext>(options =>
[fa0fbaf]67 options.UseNpgsql(dataSource));
[08aefc6]68
[fa0fbaf]69// ==========================================
70// SESSION
71// ==========================================
72
73builder.Services.AddSession(options =>
74{
75 options.IdleTimeout = TimeSpan.FromHours(2);
76 options.Cookie.HttpOnly = true;
77 options.Cookie.IsEssential = true;
78});
[08aefc6]79
80var app = builder.Build();
81
[fa0fbaf]82// ==========================================
83// MIDDLEWARE
84// ==========================================
85
[08aefc6]86if (!app.Environment.IsDevelopment())
87{
88 app.UseExceptionHandler("/Home/Error");
89 app.UseHsts();
90}
91
92app.UseHttpsRedirection();
[fa0fbaf]93
[08aefc6]94app.UseStaticFiles();
95
96app.UseRouting();
97
98app.UseSession();
99
[fa0fbaf]100app.UseAuthentication();
101
[08aefc6]102app.UseAuthorization();
103
104app.MapControllerRoute(
105 name: "default",
106 pattern: "{controller=Home}/{action=Index}/{id?}");
107
[fa0fbaf]108app.Run();
Note: See TracBrowser for help on using the repository browser.