forked from FurkanBaran/TowFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
128 lines (98 loc) · 3.37 KB
/
Program.cs
File metadata and controls
128 lines (98 loc) · 3.37 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
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using TowFinder.Data;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
// Servisleri ekleyin.
builder.Services.AddControllersWithViews();
builder.Services.AddSession(); // Session ekle
// DbContext'i MySQL ile yapılandır
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(connectionString,
new MySqlServerVersion(new Version(8, 0, 23))));
// Identity ve Cookie Authentication ekleyin
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/TowOperator/Login";
options.AccessDeniedPath = "/TowOperator/Login";
});
builder.Services.Configure<IdentityOptions>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 6;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
options.Password.RequiredUniqueChars = 1;
});
var app = builder.Build();
// Rol ve admin kullanıcı ekle
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try // Hata oluþursa uygulamayı durdurma
{
var context = services.GetRequiredService<ApplicationDbContext>();
context.Database.Migrate();
await SeedAdminUser(services);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
// HTTP isteği yapılandırması
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication(); // Authentication kullan
app.UseAuthorization();
app.UseSession(); // Session kullan
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
async Task SeedAdminUser(IServiceProvider serviceProvider)
{
var userManager = serviceProvider.GetRequiredService<UserManager<IdentityUser>>();
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
if (!await roleManager.RoleExistsAsync("Admin")) // Admin rolü yoksa oluþtur
{
await roleManager.CreateAsync(new IdentityRole("Admin"));
}
if (!await roleManager.RoleExistsAsync("TowOperator")) // TowOperator rolü yoksa oluştur
{
await roleManager.CreateAsync(new IdentityRole("TowOperator"));
}
var adminUser = new IdentityUser
{
UserName = "admin",
Email = "admin@towfinder.com",
EmailConfirmed = true
};
var user = await userManager.FindByNameAsync(adminUser.UserName);
if (user == null) // Kullanıcı yoksa oluştur
{
var result = await userManager.CreateAsync(adminUser, "Admin123!"); // Admin123! şifresini kullan
if (result.Succeeded)
{
await userManager.AddToRoleAsync(adminUser, "Admin"); // Admin rolünü ata
}
}
else // Kullanıcı varsa rolü kontrol et
{
if (!await userManager.IsInRoleAsync(user, "Admin"))
{
await userManager.AddToRoleAsync(user, "Admin");
}
}
}