# Task 6: Create Seed Data ## Task Description **Files:** - Create: `src/Fengling.AuthService/Data/SeedData.cs` - Modify: `src/Fengling.AuthService/Program.cs` ## Implementation Steps ### Step 1: Create seed data class Create: `src/Fengling.AuthService/Data/SeedData.cs` ```csharp using Fengling.AuthService.Models; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; namespace Fengling.AuthService.Data; public static class SeedData { public static async Task Initialize(IServiceProvider serviceProvider) { using var scope = serviceProvider.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); var userManager = scope.ServiceProvider.GetRequiredService>(); var roleManager = scope.ServiceProvider.GetRequiredService>(); context.Database.EnsureCreated(); var adminRole = await roleManager.FindByNameAsync("Admin"); if (adminRole == null) { adminRole = new ApplicationRole { Name = "Admin", Description = "System administrator", CreatedTime = DateTime.UtcNow }; await roleManager.CreateAsync(adminRole); } var adminUser = await userManager.FindByNameAsync("admin"); if (adminUser == null) { adminUser = new ApplicationUser { UserName = "admin", Email = "admin@fengling.local", RealName = "系统管理员", Phone = "13800138000", TenantId = 1, EmailConfirmed = true, IsDeleted = false, CreatedTime = DateTime.UtcNow }; var result = await userManager.CreateAsync(adminUser, "Admin@123"); if (result.Succeeded) { await userManager.AddToRoleAsync(adminUser, "Admin"); } } var testUser = await userManager.FindByNameAsync("testuser"); if (testUser == null) { testUser = new ApplicationUser { UserName = "testuser", Email = "test@fengling.local", RealName = "测试用户", Phone = "13900139000", TenantId = 1, EmailConfirmed = true, IsDeleted = false, CreatedTime = DateTime.UtcNow }; var result = await userManager.CreateAsync(testUser, "Test@123"); if (result.Succeeded) { var userRole = new ApplicationRole { Name = "User", Description = "普通用户", CreatedTime = DateTime.UtcNow }; await roleManager.CreateAsync(userRole); await userManager.AddToRoleAsync(testUser, "User"); } } } } ``` ### Step 2: Update Program.cs to call seed data Edit: `src/Fengling.AuthService/Program.cs` (add after `var app = builder.Build();`) ```csharp using (var scope = app.Services.CreateScope()) { await Data.SeedData.Initialize(scope.ServiceProvider); } ``` ### Step 3: Run to create seed data Run: ```bash dotnet run ``` Expected: Logs show admin user created successfully ### Step 4: Test login Run: ```bash curl -X POST http://localhost:5000/api/auth/login \ -H "Content-Type: application/json" \ -d '{"userName":"admin","password":"Admin@123","tenantId":1}' ``` Expected: Returns token (placeholder for now) ### Step 5: Commit ```bash git add src/Fengling.AuthService/Data/SeedData.cs src/Fengling.AuthService/Program.cs git commit -m "feat(auth): add seed data for admin and test users" ``` ## Context This task creates initial seed data including admin and test users with default passwords. This allows immediate testing of the authentication service. **Tech Stack**: ASP.NET Core Identity ## Verification - [ ] SeedData class created - [ ] Program.cs calls seed data on startup - [ ] Admin user created: admin/Admin@123 - [ ] Test user created: testuser/Test@123 - [ ] Both users assigned to tenant 1 - [ ] Build succeeds - [ ] Committed to git ## Notes - Passwords should be changed in production - All users assigned to tenant 1 for initial setup