-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
131 lines (106 loc) · 3.45 KB
/
Program.cs
File metadata and controls
131 lines (106 loc) · 3.45 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
using Azure.Identity;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
using System.Xml.Schema;
using HockeyClubAPI.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using HockeyClubAPI.Data;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<HockeyClubContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<HockeyClubContext>()
.AddDefaultTokenProviders();
// Add Mvc service
builder.Services.AddControllersWithViews();
builder.Services.AddControllers();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication(); // Enable authentication
app.UseAuthorization(); // Enable auhorization
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
await CreateRoles(services);
await SeedUsers(services);
}
catch (Exception ex)
{
Console.WriteLine($"An error occured while seeding roles and users: {ex.Message}");
}
}
app.Run();
async Task CreateRoles(IServiceProvider serviceProvider)
{
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
// Define Roles
string[] roleNames = { "Admin", "Leader", "Office", "Trainer", "Helper"};
IdentityResult roleResult;
foreach (var roleName in roleNames)
{
var roleExist = await roleManager.RoleExistsAsync(roleName);
if (!roleExist)
{
roleResult = await roleManager.CreateAsync(new IdentityRole(roleName));
if (!roleResult.Succeeded)
{
throw new Exception($"Failed to create role : {roleName}");
}
}
}
}
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
await CreateRoles(services);
}
async Task SeedUsers(IServiceProvider serviceProvider)
{
// Email, Password, Role/username
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var users = new List<(string Email, string Password, string Role)>
{
("admin@hockey.com", "Admin123", "Admin"),
("leader@hockey.com", "Leader123", "Leader"),
("office@hockey.com", "office123", "office"),
("trainer@hockey.com", "Trainer123", "Trainer"),
("helper@hockey.com", "Helper123", "Helper")
};
foreach (var user in users)
{
if (await userManager.FindByEmailAsync(user.Email) == null)
{
var appUser = new ApplicationUser
{
UserName = user.Email,
Email = user.Email
};
var result = await userManager.CreateAsync(appUser, user.Password);
if (result.Succeeded)
{
await userManager.AddToRoleAsync(appUser, user.Role);
}
}
}
}
using (var scope = app.Services.CreateAsyncScope())
{
var services = scope.ServiceProvider;
await CreateRoles(services);
await SeedUsers(services);
}