Add seeder.

This commit is contained in:
2024-05-05 02:25:34 +01:00
parent 8e920440a1
commit 31377e4f3f
2 changed files with 44 additions and 0 deletions

34
Aaru.Server.New/Seeder.cs Normal file
View 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);
}
}