-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·97 lines (78 loc) · 3.25 KB
/
Program.cs
File metadata and controls
executable file
·97 lines (78 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using vnMentor.Data;
using vnMentor.Models;
using vnMentor.Services;
using vnMentor.Utils;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ??
throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<DefaultDBContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddIdentity<AspNetUsers, AspNetRoles>(o =>
{
o.User.AllowedUserNameCharacters = null;
o.User.RequireUniqueEmail = true;
o.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
o.Lockout.MaxFailedAccessAttempts = 5;
o.Lockout.AllowedForNewUsers = true;
}).AddEntityFrameworkStores<DefaultDBContext>().AddDefaultTokenProviders();
builder.Services.AddHttpContextAccessor(); //include this for the light/dark mode cookie
builder.Services.AddDistributedMemoryCache();
builder.Services.AddControllersWithViews().AddNewtonsoftJson(options => { options.UseMemberCasing(); });
builder.Services.AddControllersWithViews(options =>
{
options.Filters.Add<UserProfilePictureActionFilter>(); //set ViewBag.Avatar
});
builder.Services.AddScoped<ErrorLoggingService>();
builder.Services.AddScoped<Util>();
// Add localization services
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
// Register the IStringLocalizerFactory service
builder.Services.AddSingleton<IStringLocalizerFactory, ResourceManagerStringLocalizerFactory>();
//lowercase routing
builder.Services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
builder.Services.Configure<RouteOptions>(options =>
{
options.ConstraintMap["hyphenated"] = typeof(HyphenatedRouteConstraint);
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Account}/{action=Login}/{id?}");
//app.MapRazorPages();
app.Use(async (context, next) =>
{
string? cookie = "";
if (context.Request.Cookies.TryGetValue("Language", out cookie))
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie);
}
else
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("vi");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("vi");
}
await next.Invoke();
});
app.Run();