Template
mirror of
https://github.com/egmont11/ASP.Net-Core-MVC-Template.git
synced 2026-07-24 07:09:56 +02:00
Changed Auth to use Identity, with easy to set passsword requirenments via appsettings.json
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TemplateWeb.Models.DbModels;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TemplateWeb.Entities;
|
||||
|
||||
namespace TemplateWeb.Data;
|
||||
|
||||
public class AppDbContext : DbContext
|
||||
public class AppDbContext : IdentityDbContext<UserEntity>
|
||||
{
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public DbSet<UserModel> Users { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,27 +1,33 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TemplateWeb.Models.DbModels;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using TemplateWeb.Entities;
|
||||
|
||||
namespace TemplateWeb.Data;
|
||||
|
||||
public static class DataSeeder
|
||||
{
|
||||
public static async Task SeedAdminUser(AppDbContext context)
|
||||
public static async Task SeedAsync(UserManager<UserEntity> userManager, RoleManager<IdentityRole> roleManager)
|
||||
{
|
||||
// Only run if no users exist
|
||||
if (await context.Users.AnyAsync())
|
||||
// Ensure roles exist
|
||||
foreach (var role in new[] { "Admin", "User" })
|
||||
{
|
||||
return;
|
||||
if (!await roleManager.RoleExistsAsync(role))
|
||||
await roleManager.CreateAsync(new IdentityRole(role));
|
||||
}
|
||||
|
||||
var admin = new UserModel
|
||||
// Seed initial admin user if none exists
|
||||
if (!userManager.Users.Any())
|
||||
{
|
||||
UserName = "admin",
|
||||
Email = "admin@template.com",
|
||||
Password = BCrypt.Net.BCrypt.HashPassword("Admin123!"), // You should change this on first login
|
||||
Role = UserRole.Admin
|
||||
};
|
||||
var admin = new UserEntity
|
||||
{
|
||||
UserName = "admin",
|
||||
Email = "admin@template.com",
|
||||
EmailConfirmed = true,
|
||||
};
|
||||
|
||||
await context.Users.AddAsync(admin);
|
||||
await context.SaveChangesAsync();
|
||||
// You should change this password on first login
|
||||
var result = await userManager.CreateAsync(admin, "Admin123!");
|
||||
if (result.Succeeded)
|
||||
await userManager.AddToRoleAsync(admin, "Admin");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user