-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
156 lines (144 loc) · 5.19 KB
/
Program.cs
File metadata and controls
156 lines (144 loc) · 5.19 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
145
146
147
148
149
150
151
152
153
154
155
156
using BlogReview.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using BlogReview.Models;
using Microsoft.AspNetCore.Localization;
using System.Globalization;
using BlogReview.Controllers;
using BlogReview.Services;
using CloudinaryDotNet;
var builder = WebApplication.CreateBuilder(args);
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
var connectionString = configuration.GetConnectionString("DefaultConnection");
builder.Services.AddLocalization(o => o.ResourcesPath = "Resources");
builder.Services.AddControllersWithViews()
.AddViewLocalization();
builder.Services.AddDbContext<ArticleContext>(options =>
options.UseLazyLoadingProxies()
.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 31))));
builder.Services.AddDefaultIdentity<User>(options =>
{
options.SignIn.RequireConfirmedAccount = false;
options.User.RequireUniqueEmail = true;
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+ ";
})
.AddRoles<IdentityRole<Guid>>()
.AddEntityFrameworkStores<ArticleContext>();
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Account/Login";
});
builder.Services.AddSignalR();
builder.Services.AddAuthentication()
.AddLinkedIn(options =>
{
IConfigurationSection linkedinAuthNSection =
builder.Configuration.GetSection("Authentication:Linkedin");
options.ClientId = linkedinAuthNSection["ClientId"];
options.ClientSecret = linkedinAuthNSection["ClientSecret"];
options.SignInScheme = IdentityConstants.ExternalScheme;
})
.AddGoogle(options =>
{
IConfigurationSection googleAuthNSection =
builder.Configuration.GetSection("Authentication:Google");
options.ClientId = googleAuthNSection["ClientId"];
options.ClientSecret = googleAuthNSection["ClientSecret"];
options.SignInScheme = IdentityConstants.ExternalScheme;
});
builder.Services.AddScoped(provider =>
{
IConfigurationSection config =
builder.Configuration.GetSection("ImageCloud:Cloudinary");
return new ImageStorageService(new Account(config["CloudName"], config["Key"], config["Secret"]));
});
builder.Services.AddScoped<UserService>();
builder.Services.AddScoped<ArticleStorageService>();
builder.Services.AddScoped<TagService>();
builder.Services.AddScoped<LikeService>();
builder.Services.AddScoped<CommentService>();
builder.Services.AddScoped<RatingService>();
var app = builder.Build();
app.UseCookiePolicy(new CookiePolicyOptions()
{
MinimumSameSitePolicy = SameSiteMode.Lax
});
using var scope = app.Services.CreateScope();
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole<Guid>>>();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<User>>();
if (!await roleManager.RoleExistsAsync("MasterAdmin"))
{
var role = new IdentityRole<Guid> { Name = "MasterAdmin" };
var result = await roleManager.CreateAsync(role);
if (!result.Succeeded)
{
throw new Exception("Failed to create the MasterAdmin role.");
}
}
if (!await roleManager.RoleExistsAsync("Admin"))
{
var adminRole = new IdentityRole<Guid> { Name = "Admin" };
var result = await roleManager.CreateAsync(adminRole);
if (!result.Succeeded)
{
throw new Exception("Failed to create the Admin role.");
}
else
{
var admins = builder.Configuration.GetSection("DefaultAdmins");
foreach (var admin in admins.GetChildren())
{
User userAdmin = new() { UserName = admin["UserName"], Email = admin["Email"] };
var adminRes = await userManager.CreateAsync(userAdmin);
if (adminRes.Succeeded)
{
await userManager.AddToRoleAsync(userAdmin, "Admin");
await userManager.AddToRoleAsync(userAdmin, "MasterAdmin");
}
else
{
throw new Exception("Failed to add user as an Admin.");
}
}
}
}
if (!await roleManager.RoleExistsAsync("User"))
{
var role = new IdentityRole<Guid> { Name = "User" };
var result = await roleManager.CreateAsync(role);
if (!result.Succeeded)
{
throw new Exception("Failed to create the User role.");
}
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
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();
}
var supportedCultures = new[]
{
new CultureInfo("en"),
new CultureInfo("ru")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures,
RequestCultureProviders = new[] { new CookieRequestCultureProvider() }
});
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapHub<CommentsHub>("/comment");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Feed}/{action=Index}/{id?}");
app.Run();