|
| 1 | +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Linq; |
| 7 | +using System.Security.Claims; |
| 8 | + |
| 9 | +using Microsoft.AspNetCore.Identity; |
| 10 | +using Microsoft.EntityFrameworkCore; |
| 11 | +using Microsoft.Extensions.DependencyInjection; |
| 12 | + |
| 13 | +using StsServerIdentity.Data; |
| 14 | +using StsServerIdentity.Models; |
| 15 | + |
| 16 | +using IdentityModel; |
| 17 | + |
| 18 | +namespace StsServerIdentity |
| 19 | +{ |
| 20 | + public class SeedData |
| 21 | + { |
| 22 | + public static void EnsureSeedData(IServiceProvider serviceProvider) |
| 23 | + { |
| 24 | + using (var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope()) |
| 25 | + { |
| 26 | + var context = scope.ServiceProvider.GetService<ApplicationDbContext>(); |
| 27 | + context.Database.Migrate(); |
| 28 | + |
| 29 | + var userMgr = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>(); |
| 30 | + var alice = userMgr.FindByNameAsync("alice").Result; |
| 31 | + if (alice == null) |
| 32 | + { |
| 33 | + alice = new ApplicationUser |
| 34 | + { |
| 35 | + UserName = "alice" |
| 36 | + }; |
| 37 | + var result = userMgr.CreateAsync(alice, "Pass123$").Result; |
| 38 | + if (!result.Succeeded) |
| 39 | + { |
| 40 | + throw new Exception(result.Errors.First().Description); |
| 41 | + } |
| 42 | + |
| 43 | + result = userMgr.AddClaimsAsync(alice, new Claim[]{ |
| 44 | + new Claim(JwtClaimTypes.Name, "Alice Smith"), |
| 45 | + new Claim(JwtClaimTypes.GivenName, "Alice"), |
| 46 | + new Claim(JwtClaimTypes.FamilyName, "Smith"), |
| 47 | + new Claim(JwtClaimTypes.Email, "[email protected]"), |
| 48 | + new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean), |
| 49 | + new Claim(JwtClaimTypes.WebSite, "http://alice.com"), |
| 50 | + new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServer4.IdentityServerConstants.ClaimValueTypes.Json) |
| 51 | + }).Result; |
| 52 | + if (!result.Succeeded) |
| 53 | + { |
| 54 | + throw new Exception(result.Errors.First().Description); |
| 55 | + } |
| 56 | + Console.WriteLine("alice created"); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + Console.WriteLine("alice already exists"); |
| 61 | + } |
| 62 | + |
| 63 | + var bob = userMgr.FindByNameAsync("bob").Result; |
| 64 | + if (bob == null) |
| 65 | + { |
| 66 | + bob = new ApplicationUser |
| 67 | + { |
| 68 | + UserName = "bob" |
| 69 | + }; |
| 70 | + var result = userMgr.CreateAsync(bob, "Pass123$").Result; |
| 71 | + if (!result.Succeeded) |
| 72 | + { |
| 73 | + throw new Exception(result.Errors.First().Description); |
| 74 | + } |
| 75 | + |
| 76 | + result = userMgr.AddClaimsAsync(bob, new Claim[]{ |
| 77 | + new Claim(JwtClaimTypes.Name, "Bob Smith"), |
| 78 | + new Claim(JwtClaimTypes.GivenName, "Bob"), |
| 79 | + new Claim(JwtClaimTypes.FamilyName, "Smith"), |
| 80 | + new Claim(JwtClaimTypes.Email, "[email protected]"), |
| 81 | + new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean), |
| 82 | + new Claim(JwtClaimTypes.WebSite, "http://bob.com"), |
| 83 | + new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServer4.IdentityServerConstants.ClaimValueTypes.Json), |
| 84 | + new Claim("location", "somewhere") |
| 85 | + }).Result; |
| 86 | + if (!result.Succeeded) |
| 87 | + { |
| 88 | + throw new Exception(result.Errors.First().Description); |
| 89 | + } |
| 90 | + Console.WriteLine("bob created"); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + Console.WriteLine("bob already exists"); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments