change bd for postgres

features/IdentitySvc
Vianney JOURDY 2 weeks ago
parent 474a892a82
commit 4eb17bf7bd

@ -5,72 +5,77 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace IdentitySvc.Data.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20240123193529_Users")]
partial class Users
[Migration("20250525164319_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.0");
modelBuilder
.HasAnnotation("ProductVersion", "8.0.11")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("IdentitySvc.Models.ApplicationUser", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<int>("AccessFailedCount")
.HasColumnType("INTEGER");
.HasColumnType("integer");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("Email")
.HasMaxLength(256)
.HasColumnType("TEXT");
.HasColumnType("character varying(256)");
b.Property<bool>("EmailConfirmed")
.HasColumnType("INTEGER");
.HasColumnType("boolean");
b.Property<bool>("LockoutEnabled")
.HasColumnType("INTEGER");
.HasColumnType("boolean");
b.Property<DateTimeOffset?>("LockoutEnd")
.HasColumnType("TEXT");
.HasColumnType("timestamp with time zone");
b.Property<string>("NormalizedEmail")
.HasMaxLength(256)
.HasColumnType("TEXT");
.HasColumnType("character varying(256)");
b.Property<string>("NormalizedUserName")
.HasMaxLength(256)
.HasColumnType("TEXT");
.HasColumnType("character varying(256)");
b.Property<string>("PasswordHash")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("PhoneNumber")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<bool>("PhoneNumberConfirmed")
.HasColumnType("INTEGER");
.HasColumnType("boolean");
b.Property<string>("SecurityStamp")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<bool>("TwoFactorEnabled")
.HasColumnType("INTEGER");
.HasColumnType("boolean");
b.Property<string>("UserName")
.HasMaxLength(256)
.HasColumnType("TEXT");
.HasColumnType("character varying(256)");
b.HasKey("Id");
@ -87,19 +92,19 @@ namespace IdentitySvc.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("Name")
.HasMaxLength(256)
.HasColumnType("TEXT");
.HasColumnType("character varying(256)");
b.Property<string>("NormalizedName")
.HasMaxLength(256)
.HasColumnType("TEXT");
.HasColumnType("character varying(256)");
b.HasKey("Id");
@ -114,17 +119,19 @@ namespace IdentitySvc.Data.Migrations
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ClaimType")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("ClaimValue")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("RoleId")
.IsRequired()
.HasColumnType("TEXT");
.HasColumnType("text");
b.HasKey("Id");
@ -137,17 +144,19 @@ namespace IdentitySvc.Data.Migrations
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ClaimType")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("ClaimValue")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("UserId")
.IsRequired()
.HasColumnType("TEXT");
.HasColumnType("text");
b.HasKey("Id");
@ -159,17 +168,17 @@ namespace IdentitySvc.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("ProviderKey")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("ProviderDisplayName")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("UserId")
.IsRequired()
.HasColumnType("TEXT");
.HasColumnType("text");
b.HasKey("LoginProvider", "ProviderKey");
@ -181,10 +190,10 @@ namespace IdentitySvc.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("RoleId")
.HasColumnType("TEXT");
.HasColumnType("text");
b.HasKey("UserId", "RoleId");
@ -196,16 +205,16 @@ namespace IdentitySvc.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.Property<string>("UserId")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("LoginProvider")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("Name")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("Value")
.HasColumnType("TEXT");
.HasColumnType("text");
b.HasKey("UserId", "LoginProvider", "Name");

@ -1,12 +1,13 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace IdentitySvc.Data.Migrations
{
/// <inheritdoc />
public partial class Users : Migration
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
@ -15,10 +16,10 @@ namespace IdentitySvc.Data.Migrations
name: "AspNetRoles",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
Name = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
NormalizedName = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
ConcurrencyStamp = table.Column<string>(type: "TEXT", nullable: true)
Id = table.Column<string>(type: "text", nullable: false),
Name = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
NormalizedName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
ConcurrencyStamp = table.Column<string>(type: "text", nullable: true)
},
constraints: table =>
{
@ -29,21 +30,21 @@ namespace IdentitySvc.Data.Migrations
name: "AspNetUsers",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
UserName = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
Email = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
NormalizedEmail = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(type: "INTEGER", nullable: false),
PasswordHash = table.Column<string>(type: "TEXT", nullable: true),
SecurityStamp = table.Column<string>(type: "TEXT", nullable: true),
ConcurrencyStamp = table.Column<string>(type: "TEXT", nullable: true),
PhoneNumber = table.Column<string>(type: "TEXT", nullable: true),
PhoneNumberConfirmed = table.Column<bool>(type: "INTEGER", nullable: false),
TwoFactorEnabled = table.Column<bool>(type: "INTEGER", nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(type: "TEXT", nullable: true),
LockoutEnabled = table.Column<bool>(type: "INTEGER", nullable: false),
AccessFailedCount = table.Column<int>(type: "INTEGER", nullable: false)
Id = table.Column<string>(type: "text", nullable: false),
UserName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
Email = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
NormalizedEmail = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(type: "boolean", nullable: false),
PasswordHash = table.Column<string>(type: "text", nullable: true),
SecurityStamp = table.Column<string>(type: "text", nullable: true),
ConcurrencyStamp = table.Column<string>(type: "text", nullable: true),
PhoneNumber = table.Column<string>(type: "text", nullable: true),
PhoneNumberConfirmed = table.Column<bool>(type: "boolean", nullable: false),
TwoFactorEnabled = table.Column<bool>(type: "boolean", nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
LockoutEnabled = table.Column<bool>(type: "boolean", nullable: false),
AccessFailedCount = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
@ -54,11 +55,11 @@ namespace IdentitySvc.Data.Migrations
name: "AspNetRoleClaims",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
RoleId = table.Column<string>(type: "TEXT", nullable: false),
ClaimType = table.Column<string>(type: "TEXT", nullable: true),
ClaimValue = table.Column<string>(type: "TEXT", nullable: true)
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
RoleId = table.Column<string>(type: "text", nullable: false),
ClaimType = table.Column<string>(type: "text", nullable: true),
ClaimValue = table.Column<string>(type: "text", nullable: true)
},
constraints: table =>
{
@ -75,11 +76,11 @@ namespace IdentitySvc.Data.Migrations
name: "AspNetUserClaims",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
UserId = table.Column<string>(type: "TEXT", nullable: false),
ClaimType = table.Column<string>(type: "TEXT", nullable: true),
ClaimValue = table.Column<string>(type: "TEXT", nullable: true)
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
UserId = table.Column<string>(type: "text", nullable: false),
ClaimType = table.Column<string>(type: "text", nullable: true),
ClaimValue = table.Column<string>(type: "text", nullable: true)
},
constraints: table =>
{
@ -96,10 +97,10 @@ namespace IdentitySvc.Data.Migrations
name: "AspNetUserLogins",
columns: table => new
{
LoginProvider = table.Column<string>(type: "TEXT", nullable: false),
ProviderKey = table.Column<string>(type: "TEXT", nullable: false),
ProviderDisplayName = table.Column<string>(type: "TEXT", nullable: true),
UserId = table.Column<string>(type: "TEXT", nullable: false)
LoginProvider = table.Column<string>(type: "text", nullable: false),
ProviderKey = table.Column<string>(type: "text", nullable: false),
ProviderDisplayName = table.Column<string>(type: "text", nullable: true),
UserId = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
@ -116,8 +117,8 @@ namespace IdentitySvc.Data.Migrations
name: "AspNetUserRoles",
columns: table => new
{
UserId = table.Column<string>(type: "TEXT", nullable: false),
RoleId = table.Column<string>(type: "TEXT", nullable: false)
UserId = table.Column<string>(type: "text", nullable: false),
RoleId = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
@ -140,10 +141,10 @@ namespace IdentitySvc.Data.Migrations
name: "AspNetUserTokens",
columns: table => new
{
UserId = table.Column<string>(type: "TEXT", nullable: false),
LoginProvider = table.Column<string>(type: "TEXT", nullable: false),
Name = table.Column<string>(type: "TEXT", nullable: false),
Value = table.Column<string>(type: "TEXT", nullable: true)
UserId = table.Column<string>(type: "text", nullable: false),
LoginProvider = table.Column<string>(type: "text", nullable: false),
Name = table.Column<string>(type: "text", nullable: false),
Value = table.Column<string>(type: "text", nullable: true)
},
constraints: table =>
{

@ -4,6 +4,7 @@ using IdentitySvc.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
@ -15,59 +16,63 @@ namespace IdentitySvc.Data.Migrations
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.0");
modelBuilder
.HasAnnotation("ProductVersion", "8.0.11")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("IdentitySvc.Models.ApplicationUser", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<int>("AccessFailedCount")
.HasColumnType("INTEGER");
.HasColumnType("integer");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("Email")
.HasMaxLength(256)
.HasColumnType("TEXT");
.HasColumnType("character varying(256)");
b.Property<bool>("EmailConfirmed")
.HasColumnType("INTEGER");
.HasColumnType("boolean");
b.Property<bool>("LockoutEnabled")
.HasColumnType("INTEGER");
.HasColumnType("boolean");
b.Property<DateTimeOffset?>("LockoutEnd")
.HasColumnType("TEXT");
.HasColumnType("timestamp with time zone");
b.Property<string>("NormalizedEmail")
.HasMaxLength(256)
.HasColumnType("TEXT");
.HasColumnType("character varying(256)");
b.Property<string>("NormalizedUserName")
.HasMaxLength(256)
.HasColumnType("TEXT");
.HasColumnType("character varying(256)");
b.Property<string>("PasswordHash")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("PhoneNumber")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<bool>("PhoneNumberConfirmed")
.HasColumnType("INTEGER");
.HasColumnType("boolean");
b.Property<string>("SecurityStamp")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<bool>("TwoFactorEnabled")
.HasColumnType("INTEGER");
.HasColumnType("boolean");
b.Property<string>("UserName")
.HasMaxLength(256)
.HasColumnType("TEXT");
.HasColumnType("character varying(256)");
b.HasKey("Id");
@ -84,19 +89,19 @@ namespace IdentitySvc.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("Name")
.HasMaxLength(256)
.HasColumnType("TEXT");
.HasColumnType("character varying(256)");
b.Property<string>("NormalizedName")
.HasMaxLength(256)
.HasColumnType("TEXT");
.HasColumnType("character varying(256)");
b.HasKey("Id");
@ -111,17 +116,19 @@ namespace IdentitySvc.Data.Migrations
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ClaimType")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("ClaimValue")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("RoleId")
.IsRequired()
.HasColumnType("TEXT");
.HasColumnType("text");
b.HasKey("Id");
@ -134,17 +141,19 @@ namespace IdentitySvc.Data.Migrations
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ClaimType")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("ClaimValue")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("UserId")
.IsRequired()
.HasColumnType("TEXT");
.HasColumnType("text");
b.HasKey("Id");
@ -156,17 +165,17 @@ namespace IdentitySvc.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("ProviderKey")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("ProviderDisplayName")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("UserId")
.IsRequired()
.HasColumnType("TEXT");
.HasColumnType("text");
b.HasKey("LoginProvider", "ProviderKey");
@ -178,10 +187,10 @@ namespace IdentitySvc.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("RoleId")
.HasColumnType("TEXT");
.HasColumnType("text");
b.HasKey("UserId", "RoleId");
@ -193,16 +202,16 @@ namespace IdentitySvc.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.Property<string>("UserId")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("LoginProvider")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("Name")
.HasColumnType("TEXT");
.HasColumnType("text");
b.Property<string>("Value")
.HasColumnType("TEXT");
.HasColumnType("text");
b.HasKey("UserId", "LoginProvider", "Name");

@ -14,7 +14,7 @@ internal static class HostingExtensions
builder.Services.AddRazorPages();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
@ -29,24 +29,19 @@ internal static class HostingExtensions
options.Events.RaiseSuccessEvents = true;
// see https://docs.duendesoftware.com/identityserver/v6/fundamentals/resources/
options.EmitStaticAudienceClaim = true;
// options.EmitStaticAudienceClaim = true;
})
.AddInMemoryIdentityResources(Config.IdentityResources)
.AddInMemoryApiScopes(Config.ApiScopes)
.AddInMemoryClients(Config.Clients)
.AddAspNetIdentity<ApplicationUser>();
builder.Services.AddAuthentication()
.AddGoogle(options =>
{
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
// register your IdentityServer with Google at https://console.developers.google.com
// enable the Google+ API
// set the redirect URI to https://localhost:5001/signin-google
options.ClientId = "copy client ID from Google here";
options.ClientSecret = "copy client secret from Google here";
});
builder.Services.ConfigureApplicationCookie(options =>
{
options.Cookie.SameSite = SameSiteMode.Lax;
});
builder.Services.AddAuthentication();
return builder.Build();
}

@ -10,12 +10,13 @@
<PackageReference Include="Duende.IdentityServer.AspNetIdentity" Version="7.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="8.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.11" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3" />
</ItemGroup>
</Project>

@ -22,13 +22,7 @@ try
// this seeding is only for the template to bootstrap the DB and users.
// in production you will likely want a different approach.
if (args.Contains("/seed"))
{
Log.Information("Seeding database...");
SeedData.EnsureSeedData(app);
Log.Information("Done seeding database. Exiting.");
return;
}
SeedData.EnsureSeedData(app);
app.Run();
}

@ -6,7 +6,7 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001"
"applicationUrl": "http://localhost:5001"
}
}
}

@ -12,76 +12,70 @@ public class SeedData
{
public static void EnsureSeedData(WebApplication app)
{
using (var scope = app.Services.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Database.Migrate();
using var scope = app.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
var context = scope.ServiceProvider.GetRequiredService<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);
}
var userMgr = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
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
if (userMgr.Users.Any()) return;
var alice = userMgr.FindByNameAsync("alice").Result;
if (alice == null)
{
alice = new ApplicationUser
{
Log.Debug("alice already exists");
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);
}
var bob = userMgr.FindByNameAsync("bob").Result;
if (bob == null)
result = userMgr.AddClaimsAsync(alice, new Claim[]{
new Claim(JwtClaimTypes.Name, "Alice Smith"),
}).Result;
if (!result.Succeeded)
{
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);
}
throw new Exception(result.Errors.First().Description);
}
Log.Debug("alice created");
}
else
{
Log.Debug("alice already exists");
}
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");
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);
}
else
result = userMgr.AddClaimsAsync(bob, new Claim[]{
new Claim(JwtClaimTypes.Name, "Bob Smith"),
}).Result;
if (!result.Succeeded)
{
Log.Debug("bob already exists");
throw new Exception(result.Errors.First().Description);
}
Log.Debug("bob created");
}
else
{
Log.Debug("bob already exists");
}
}
}

@ -0,0 +1 @@
{"Version":1,"Id":"77EB8A5360F8A1CEBDE8A5C6954780A5","Created":"2025-05-25T15:12:00.8996178Z","Algorithm":"RS256","IsX509Certificate":false,"Data":"CfDJ8BIJalz9mTlNqljDUzwnDk0ARrMXC6Z0QHpR_RqrdI-s1X8AxnD86UJmmJrnZRBXC0-q1fGrU2GMD1bEaR0_EnEw4GU5_fX9B1EVWs5OgQNXazOxkkCj5rnCcJmu779FXuoKbCn95KHCljQFxcAClWlRL4-7ufG5YYn3ka_3He3cHZDEt8Kh2inMFZq2jWjoxi3F668ApZQty2RmEDulv512LYH0NlKXSYgGlb6GtAHzngGjiUTIjwhKtcZ8BhgMzurm-9AZ4zLOcnnrrkA4yPgyL9omiHrkrjgbttmq107N_RsIhV9xfe5g3NqckIhRvWCPbqfzvmtiKWrkgzQJSFr73I5bYH_-9Lf10k7Jd-XMrAuf2Yts8WUNxUVw9Bv_0uSOAgMdAH0gExBKw2WxXBzc71gFUly4_THC8Od5J6CoyQIGdsSJVUskpc5z4QkkUOzoO1BoLsNL8OpkjHpuGMoQDTlC4khHqD37tJPj1cHUt5uwOrJnInmzXXSLK3mTQOtuNmbDSiNO_UJ5DnPDzvhNoPJjiTeuRWGhnd13OiC8Ehk_EIfxmSkw3Xs5v54-97MAL71DD0QYurWc2lnXjqX4_7Kqpx0kmJpTibBHXuykVBa32lMKLroo6vSAkaKPeEFNoMZymIH5oABMVHApUs2jw_TheVwuJ0pvBhFwr4wfCa5x6IDmfuKYsglkA6JZMirI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