-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
214 lines (175 loc) · 7.42 KB
/
Program.cs
File metadata and controls
214 lines (175 loc) · 7.42 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MusicLibraryApp;
using MusicLibraryApp.Areas.Identity.Data;
using MusicLibraryApp.Data;
using MusicLibraryApp.Controllers;
using AspNetCoreRateLimit;
using MusicLibraryApp.Service;
using System.Reflection;
using System.Globalization;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Options;
using AutoMapper;
using NLog;
using NLog.Web;
using System.Configuration;
using OfficeOpenXml;
public class Program
{
public static async Task Main(string[] args)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
var builder = WebApplication.CreateBuilder(args);
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Debug("init main");
try {
builder.Logging.ClearProviders();
builder.Host.UseNLog();
builder.Services.AddAutoMapper(typeof(Program));
#region Localizer
builder.Services.AddSingleton<LanguageService>();
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddMvc()
.AddViewLocalization()
.AddDataAnnotationsLocalization(options => options.DataAnnotationLocalizerProvider=(type,factory)=>
{
var assemblyName = new AssemblyName(typeof(SharedResource).GetTypeInfo().Assembly.FullName);
return factory.Create(nameof(SharedResource),assemblyName.Name);
});
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("tr-TR"),
};
options.DefaultRequestCulture = new RequestCulture(culture: "tr-TR",uiCulture:"tr-TR");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders.Insert(0,new QueryStringRequestCultureProvider());
});
var connectionString = builder.Configuration.GetConnectionString("AuthDbContextConnection") ??
throw new InvalidOperationException("Connection string 'AuthDbContextConnection' not found.");
builder.Services.AddDbContext<AuthDbContext>(options => options.UseSqlServer(connectionString));
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<AuthDbContext>();
builder.Services.AddTransient<IEmailService, RabbitMQService>();
builder.Services.AddHttpClient();
builder.Services.AddSingleton<LastFmService>();
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
builder.Services.Configure<IdentityOptions>(options => {
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequiredLength = 6;
});
// Rate Limit Kütphane Konfigürasyonlarý
builder.Services.AddMemoryCache();
builder.Services.Configure<IpRateLimitOptions>(options =>
{
options.GeneralRules = new List<RateLimitRule>
{
new RateLimitRule
{
Endpoint = "*",
Limit = 200,
Period = "1m" // 5 dakikada 5 istek
}
};
});
builder.Services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
builder.Services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
builder.Services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
builder.Services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
// Session Kütüphanesi Konfigürasyonlarý
builder.Services.AddSession();
var app = builder.Build();
app.UseIpRateLimiting();
// 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();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRequestLocalization(app.Services.GetRequiredService<IOptions<RequestLocalizationOptions>>().Value);
app.UseRouting();
app.UseAuthorization();
app.UseSession();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
using (var scope = app.Services.CreateScope())
{
var RoleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var roles = new[] {
"Admin",
"Moderator",
"Verified",//Onaylý kullanýcý (Admin tarafýndan onaylanmýþ)
"User",
"Rejected User" //Reddedilmiþ kullanýcý (Admin tarafýndan reddedilmiþ)
};
foreach (var role in roles)
{
if (!await RoleManager.RoleExistsAsync(role))
await RoleManager.CreateAsync(new IdentityRole(role));
}
}
using (var scope = app.Services.CreateScope())
{
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
string adminEmail = "admin@admin.com";
string adminPassword = "Admin123!";
string moderatorEmail = "moderator@moderator.com";
string moderatorPassword = "moderator123";
// admin user hesabý yoksa oluþtur
if (await userManager.FindByEmailAsync(adminEmail) == null)
{
var adminUser = new ApplicationUser
{
Email = adminEmail,
UserName = adminEmail,
FirstName = "Admin",
LastName = "Admin",
};
await userManager.CreateAsync(adminUser, adminPassword);
await userManager.AddToRoleAsync(adminUser, "Admin");
}
// Moderator user hesabý yoksa oluþtur
if (await userManager.FindByEmailAsync(moderatorEmail) == null)
{
var moderatorUser = new ApplicationUser
{
Email = moderatorEmail,
UserName = moderatorEmail,
FirstName = "Moderator",
LastName = "Moderator",
};
await userManager.CreateAsync(moderatorUser, moderatorPassword);
await userManager.AddToRoleAsync(moderatorUser, "Moderator");
}
}
app.Run();
}
catch (Exception ex)
{
logger.Error(ex);
}
finally
{
LogManager.Shutdown();
}
}
}
#endregion