-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (91 loc) · 3.75 KB
/
Copy pathProgram.cs
File metadata and controls
110 lines (91 loc) · 3.75 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
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using web_programming_project_2025_bthnozdemir.Models;
var builder = WebApplication.CreateBuilder(args);
// SQLite Veritabanı Bağlantısı
builder.Services.AddDbContext<Context>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
// FILE UPLOAD LİMİTİ - RESİM SORUNUNU ÇÖZER (10MB)
builder.Services.Configure<Microsoft.AspNetCore.Http.Features.FormOptions>(options =>
{
options.MultipartBodyLengthLimit = 10 * 1024 * 1024; // 10MB limit
});
// Identity Yapılandırması (Kullanıcı & Rol) ASP.NET Core Identitiy
// Uygulama ayağa kalkarken Admin ve User rolleri seed ediliyor, admin kullanıcı otomatik oluşturuluyor.
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.SignIn.RequireConfirmedAccount = false;
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequiredLength = 3;
})
.AddEntityFrameworkStores<Context>()
.AddDefaultTokenProviders();
// Çerez ve Erişim Yönlendirmeleri
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
options.AccessDeniedPath = "/Account/AccessDenied";
});
// Servis Enjeksiyonları (Dependency Injection)
// İş mantığını Controller'den ayırmak için yazdığımız Service katmanını sisteme dahil ediyoruz.
builder.Services.AddScoped<web_programming_project_2025_bthnozdemir.Services.CurrencyService>();
builder.Services.AddScoped<web_programming_project_2025_bthnozdemir.Services.ProductService>();
builder.Services.AddScoped<web_programming_project_2025_bthnozdemir.Services.CategoryService>();
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Başlangıç Verileri ve Admin Hesabı Tanımlama
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
// Roller
if (!await roleManager.RoleExistsAsync("Admin")) await roleManager.CreateAsync(new IdentityRole("Admin"));
if (!await roleManager.RoleExistsAsync("User")) await roleManager.CreateAsync(new IdentityRole("User"));
// Admin Kullanıcısı
var adminEmail = "b231200372@ogr.sakarya.edu.tr";
var adminUser = await userManager.FindByEmailAsync(adminEmail);
if (adminUser == null)
{
adminUser = new ApplicationUser
{
UserName = adminEmail,
Email = adminEmail,
EmailConfirmed = true,
FirstName = "Batuhan",
LastName = "Özdemir"
};
var result = await userManager.CreateAsync(adminUser, "bsm");
if (result.Succeeded)
{
await userManager.AddToRoleAsync(adminUser, "Admin");
}
}
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "Seed Data yüklenirken hata oluştu.");
}
}
// HTTP İstek Hattı (Middleware)
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();