-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebApplicationExtensions.cs
35 lines (28 loc) · 1.25 KB
/
WebApplicationExtensions.cs
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
using Gambling_my_beloved.Models;
using Microsoft.AspNetCore.Identity;
namespace Gambling_my_beloved;
public static class WebApplicationExtensions
{
public static async Task<WebApplication> CreateRolesAsync(this WebApplication app)
{
using IServiceScope scope = app.Services.CreateScope();
RoleManager<IdentityRole> roleManager = (RoleManager<IdentityRole>)scope.ServiceProvider.GetService(typeof(RoleManager<IdentityRole>));
List<string> roles = Roles.AllRoles;
foreach (string role in roles)
{
if (!await roleManager.RoleExistsAsync(role))
await roleManager.CreateAsync(new(role));
}
return app;
}
public static async Task<WebApplication> CreateMasterUserAsync(this WebApplication app)
{
using IServiceScope scope = app.Services.CreateScope();
UserManager<ApplicationUser> userManager =
(UserManager<ApplicationUser>)scope.ServiceProvider.GetService(typeof(UserManager<ApplicationUser>));
// find user by id
ApplicationUser user = await userManager.FindByIdAsync("d86d5ce1-98ea-43b5-a63c-baf0264bdbed");
await userManager.AddToRoleAsync(user, Roles.Administrator);
return app;
}
}