-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
144 lines (112 loc) · 4.33 KB
/
Program.cs
File metadata and controls
144 lines (112 loc) · 4.33 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using AspNetCore.Identity.Mongo;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Localization;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using MyBlog.DB;
using MyBlog.DB.Entities;
using MyBlog.Repositories;
using MyBlog.Repositories.Interfaces;
using MyBlog.Services;
using MyBlog.Services.Interfaces;
using System.Globalization;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services
.AddControllersWithViews()
.AddViewLocalization()
.AddDataAnnotationsLocalization();
builder.Services.AddHttpClient();
// DB Context
builder.Services.AddScoped<MyBlogContext>();
// Identity Auth
builder.Services.AddIdentityMongoDbProvider<ApplicationUser, ApplicationRole>(
identityOptions =>
{
},
mongoOptions =>
{
mongoOptions.ConnectionString = builder.Configuration["Database:MongoDB:Url"] ?? string.Empty;
mongoOptions.MigrationCollection = "auths";
});
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/admin/auth/login";
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
});
// Repositories
builder.Services.AddScoped<IIntroductionRepository, IntroductionRepository>();
builder.Services.AddScoped<IPersonalRepository, PersonalRepository>();
builder.Services.AddScoped<IProjectRepository, ProjectRepository>();
builder.Services.AddScoped<IFeedbackRepository, FeedbackRepository>();
builder.Services.AddScoped<IToolRepository, ToolRepository>();
// Services
builder.Services.AddScoped<ICVService, CVService>();
builder.Services.AddScoped<IHomeService, HomeService>();
builder.Services.AddScoped<IProjectService, ProjectService>();
builder.Services.AddScoped<IToolService, ToolService>();
builder.Services.AddScoped<IFeedbackService, FeedbackService>();
builder.Services.AddScoped<ICaptchaService, CaptchaService>();
builder.Services.AddScoped<IAccountService, AccountService>();
var app = builder.Build();
// Multi Languages
CultureInfo viCulture = new CultureInfo("vi");
viCulture.DateTimeFormat.ShortTimePattern = "HH:mm";
viCulture.DateTimeFormat.LongTimePattern = "HH:mm:ss";
CultureInfo enCulture = new CultureInfo("en");
enCulture.DateTimeFormat.ShortTimePattern = "HH:mm";
enCulture.DateTimeFormat.LongTimePattern = "HH:mm:ss";
var supportedCultures = new[] { enCulture, viCulture };
var localizationOptions = new RequestLocalizationOptions()
.SetDefaultCulture("vi")
.AddSupportedCultures(supportedCultures.Select(c => c.Name).ToArray())
.AddSupportedUICultures(supportedCultures.Select(c => c.Name).ToArray());
app.UseRequestLocalization(localizationOptions);
// For testing only
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
using var scope = app.Services.CreateScope();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
var adminEmail = "admin@example.com";
var adminUser = await userManager.FindByEmailAsync(adminEmail);
if (adminUser == null)
{
adminUser = new ApplicationUser
{
UserName = adminEmail,
Email = adminEmail,
EmailConfirmed = true
};
var result = await userManager.CreateAsync(adminUser, "1234");
if (result.Succeeded)
{
await userManager.ResetAuthenticatorKeyAsync(adminUser);
var key = await userManager.GetAuthenticatorKeyAsync(adminUser);
logger.LogInformation("======================================");
logger.LogInformation("Admin account: {Email}", adminEmail);
logger.LogInformation("Admin password: 1234");
logger.LogInformation("Admin 2FA key: {Key}", key);
logger.LogInformation("======================================");
}
}
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapAreaControllerRoute(
areaName: "Admin",
name: "Admin",
pattern: "Admin/{controller=Home}/{action=Index}/{id?}"
);
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
await app.RunAsync();