-
Notifications
You must be signed in to change notification settings - Fork 57
/
AppIdentityDbContextSeed.cs
65 lines (51 loc) · 2.7 KB
/
AppIdentityDbContextSeed.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
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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DevBetterWeb.Core;
using DevBetterWeb.Infrastructure.Identity.Data;
using Microsoft.AspNetCore.Identity;
namespace DevBetterWeb.Web.Areas.Identity;
public class AppIdentityDbContextSeed
{
public static async Task SeedAsync(UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager)
{
await roleManager.CreateAsync(new IdentityRole(AuthConstants.Roles.ADMINISTRATORS));
await roleManager.CreateAsync(new IdentityRole(AuthConstants.Roles.MEMBERS));
await roleManager.CreateAsync(new IdentityRole(AuthConstants.Roles.ALUMNI));
var defaultUser = await CreateUser(userManager, AuthConstants.Users.Demo.EMAIL);
await userManager.AddToRoleAsync(defaultUser, AuthConstants.Roles.MEMBERS);
var defaultUser2 = await CreateUser(userManager, AuthConstants.Users.Demo2.EMAIL);
await userManager.AddToRoleAsync(defaultUser2, AuthConstants.Roles.MEMBERS);
var defaultUser3 = await CreateUser(userManager, AuthConstants.Users.Demo3.EMAIL);
await userManager.AddToRoleAsync(defaultUser3, AuthConstants.Roles.MEMBERS);
var defaultUser4 = await CreateUser(userManager, AuthConstants.Users.Demo4.EMAIL);
await userManager.AddToRoleAsync(defaultUser4, AuthConstants.Roles.MEMBERS);
var nonMember = await CreateUser(userManager, AuthConstants.Users.NonMember.EMAIL);
await userManager.AddToRoleAsync(nonMember, AuthConstants.Roles.MEMBERS);
var adminUser = await CreateUser(userManager, AuthConstants.Users.Admin.EMAIL);
await userManager.AddToRoleAsync(adminUser, AuthConstants.Roles.ADMINISTRATORS);
var alumniUser = await CreateUser(userManager, AuthConstants.Users.Alumni.EMAIL);
await userManager.AddToRoleAsync(alumniUser, AuthConstants.Roles.ALUMNI);
var alumniUser2 = await CreateUser(userManager, AuthConstants.Users.Alumni2.EMAIL);
await userManager.AddToRoleAsync(alumniUser2, AuthConstants.Roles.ALUMNI);
}
public static async Task<bool> RemoveUserFromRoleAsync(UserManager<ApplicationUser> userManager, string email, string roleName)
{
var user = await userManager.FindByEmailAsync(email);
if (user == null)
{
return false;
}
await userManager.RemoveFromRolesAsync(user, new List<string> { roleName });
return true;
}
private static async Task<ApplicationUser> CreateUser(UserManager<ApplicationUser> userManager,
string userName)
{
var user = new ApplicationUser { UserName = userName, Email = userName, EmailConfirmed = true, DateCreated = DateTime.UtcNow };
await userManager.CreateAsync(user, AuthConstants.DEFAULT_PASSWORD);
var newUser = await userManager.FindByNameAsync(userName!);
return newUser!;
}
}