mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
Add seeder.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Diagnostics;
|
||||
using Aaru.CommonTypes.Interop;
|
||||
using Aaru.Server.New;
|
||||
using Aaru.Server.New.Components;
|
||||
using Aaru.Server.New.Components.Account;
|
||||
using Blazorise;
|
||||
@@ -158,6 +159,15 @@ using(IServiceScope scope = app.Services.CreateScope())
|
||||
await context.Database.MigrateAsync();
|
||||
stopwatch.Stop();
|
||||
|
||||
Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m",
|
||||
stopwatch.Elapsed.TotalSeconds);
|
||||
|
||||
stopwatch.Restart();
|
||||
Console.WriteLine("\u001b[31;1mSeeding Identity...\u001b[0m");
|
||||
await Seeder.SeedAsync(context, services);
|
||||
context.Database.Migrate();
|
||||
stopwatch.Stop();
|
||||
|
||||
Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m",
|
||||
stopwatch.Elapsed.TotalSeconds);
|
||||
}
|
||||
|
||||
34
Aaru.Server.New/Seeder.cs
Normal file
34
Aaru.Server.New/Seeder.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using DbContext = Aaru.Server.Database.DbContext;
|
||||
|
||||
namespace Aaru.Server.New;
|
||||
|
||||
public static class Seeder
|
||||
{
|
||||
public static async Task SeedAsync(DbContext ctx, IServiceProvider serviceProvider)
|
||||
{
|
||||
var email = "claunia@claunia.com";
|
||||
var randChars = new char[16];
|
||||
UserManager<IdentityUser> userManager = serviceProvider.GetRequiredService<UserManager<IdentityUser>>();
|
||||
var rnd = new Random();
|
||||
|
||||
for(var i = 0; i < randChars.Length; i++) randChars[i] = (char)rnd.Next(32, 126);
|
||||
|
||||
string password = new(randChars);
|
||||
|
||||
if(await userManager.FindByEmailAsync(email) != null) return;
|
||||
|
||||
var user = new IdentityUser
|
||||
{
|
||||
Email = email,
|
||||
NormalizedEmail = email,
|
||||
EmailConfirmed = true,
|
||||
UserName = email,
|
||||
NormalizedUserName = email
|
||||
};
|
||||
|
||||
IdentityResult result = await userManager.CreateAsync(user, password);
|
||||
|
||||
if(result.Succeeded) System.Console.WriteLine("Password is {0}, save it!", password);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user