-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
104 lines (83 loc) · 3.2 KB
/
Program.cs
File metadata and controls
104 lines (83 loc) · 3.2 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
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using StaffTraffic.Areas.Identity.Data;
using StaffTraffic.Data;
using StaffTraffic.DataAccess;
using System;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("ApplicationContextConnection") ?? throw new InvalidOperationException("Connection string 'ApplicationContextConnection' not found.");
builder.Services.AddDbContext<ApplicationContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
.AddRoles<Role>()
.AddEntityFrameworkStores<ApplicationContext>();
builder.Services.AddScoped<TrafficService>();
builder.Services.AddScoped<UserService>();
// Add services to the container.
builder.Services.AddControllersWithViews()
.AddRazorRuntimeCompilation();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
UserManager<ApplicationUser> userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
RoleManager<Role> roleManager = services.GetRequiredService<RoleManager<Role>>();
// Create roles if they don't exist
string[] roleNames = { "Admin", "User" };
IdentityResult roleResult;
foreach (var roleName in roleNames)
{
var roleExist = await roleManager.RoleExistsAsync(roleName);
if (!roleExist)
{
// Create the role and seed it with the admin user
roleResult = await roleManager.CreateAsync(new Role { Name = roleName });
}
}
// Create an admin user
var adminUser = new ApplicationUser
{
UserName = "140209",
Email = "admin@example.com",
FirstName = "Admin",
LastName = "Admin",
IsEnable = true
};
string adminPassword = "Admin@123";
var user = await userManager.FindByEmailAsync(adminUser.Email);
if (user == null)
{
// If the admin user doesn't exist, create it
var createAdminUserResult = await userManager.CreateAsync(adminUser, adminPassword);
if (createAdminUserResult.Succeeded)
{
// Assign the admin user to the "Admin" role
await userManager.AddToRoleAsync(adminUser, "Admin");
}
}
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while creating roles and admin user.");
}
}
// 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.UseRouting();
app.UseAuthentication();;
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();