-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSeedData.cs
173 lines (154 loc) · 7.15 KB
/
SeedData.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using System;
using System.Linq;
using System.Security.Claims;
using IdentityModel;
using IdentityProvider.Data;
using IdentityProvider.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
namespace IdentityProvider
{
public class SeedData
{
public static void EnsureSeedData(string connectionString)
{
var services = new ServiceCollection();
services.AddLogging();
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(connectionString));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
using (var serviceProvider = services.BuildServiceProvider())
{
using (var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var context = scope.ServiceProvider.GetService<ApplicationDbContext>();
context.Database.Migrate();
var userMgr = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var alice = userMgr.FindByNameAsync("alice").Result;
if (alice == null)
{
alice = new ApplicationUser
{
UserName = "alice",
Email = "AliceSmith@email.com",
EmailConfirmed = true,
};
var result = userMgr.CreateAsync(alice, "Pass123$").Result;
if (!result.Succeeded)
{
throw new Exception(result.Errors.First().Description);
}
result = userMgr.AddClaimsAsync(alice, new Claim[]{
new Claim(JwtClaimTypes.Name, "Alice Smith"),
new Claim(JwtClaimTypes.GivenName, "Alice"),
new Claim(JwtClaimTypes.FamilyName, "Smith"),
new Claim(JwtClaimTypes.WebSite, "http://alice.com"),
}).Result;
if (!result.Succeeded)
{
throw new Exception(result.Errors.First().Description);
}
Log.Debug("alice created");
}
else
{
Log.Debug("alice already exists");
}
var bob = userMgr.FindByNameAsync("bob").Result;
if (bob == null)
{
bob = new ApplicationUser
{
UserName = "bob",
Email = "BobSmith@email.com",
EmailConfirmed = true
};
var result = userMgr.CreateAsync(bob, "Pass123$").Result;
if (!result.Succeeded)
{
throw new Exception(result.Errors.First().Description);
}
result = userMgr.AddClaimsAsync(bob, new Claim[]{
new Claim(JwtClaimTypes.Name, "Bob Smith"),
new Claim(JwtClaimTypes.GivenName, "Bob"),
new Claim(JwtClaimTypes.FamilyName, "Smith"),
new Claim(JwtClaimTypes.WebSite, "http://bob.com"),
new Claim("location", "somewhere")
}).Result;
if (!result.Succeeded)
{
throw new Exception(result.Errors.First().Description);
}
Log.Debug("bob created");
}
else
{
Log.Debug("bob already exists");
}
var roleMgr = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var weatherUser = roleMgr.FindByNameAsync("weatheruser").Result;
if (weatherUser == null)
{
weatherUser = new IdentityRole
{
Id = "weatheruser",
Name = "weatheruser",
NormalizedName = "weatheruser"
};
var weatherUserResult = roleMgr.CreateAsync(weatherUser).Result;
if (!weatherUserResult.Succeeded)
{
throw new Exception(weatherUserResult.Errors.First().Description);
}
var aliceRoleResult = userMgr.AddToRoleAsync(alice, weatherUser.Name).Result;
if (!aliceRoleResult.Succeeded)
{
throw new Exception(aliceRoleResult.Errors.First().Description);
}
Log.Debug("weatheruser created");
}
else
{
Log.Debug("weatheruser already exists");
}
var blazorUser = roleMgr.FindByNameAsync("blazoruser").Result;
if (blazorUser == null)
{
blazorUser = new IdentityRole
{
Id = "blazoruser",
Name = "blazoruser",
NormalizedName = "blazoruser"
};
var blazorUserResult = roleMgr.CreateAsync(blazorUser).Result;
if (!blazorUserResult.Succeeded)
{
throw new Exception(blazorUserResult.Errors.First().Description);
}
var aliceRoleResult = userMgr.AddToRoleAsync(alice, blazorUser.Name).Result;
if (!aliceRoleResult.Succeeded)
{
throw new Exception(aliceRoleResult.Errors.First().Description);
}
var bobRoleResult = userMgr.AddToRoleAsync(bob, blazorUser.Name).Result;
if (!bobRoleResult.Succeeded)
{
throw new Exception(bobRoleResult.Errors.First().Description);
}
Log.Debug("blazoruser created");
}
else
{
Log.Debug("blazoruser already exists");
}
}
}
}
}
}