In this tutorial, I shall describe the steps you need to follow if you want to use Code First migration to seed both users and roles data. The seeding will be done inside the OnModelCreating() method of the Entity Framework DbContext class. To keep things simple, we will use SQLite.
In order to proceed with this tutorial you need to have the following prerequisites:
- VS Code
- You have installed .NET 8.0
- You have installed the dotnet-ef tool
Getting Started
In a terminal window, execute the following command to create an ASP.NET Razor Pages application that supports database authentication using the lightweight SQLite database:
dotnet new razor --auth individual -f net8.0 -o Code1stUsersRoles
Change directory to the newly created folder then run the application:
cd Code1stUsersRoles
dotnet watch
Click on the Register link on the top-right side of your keyboard to add a new user.
Click on the link “Click here to confirm your account” to simulate email confirmation. Thereafter, login with the newly created account email and password.
public class SeedUsersRoles {private readonly List<IdentityRole> _roles;private readonly List<IdentityUser> _users;private readonly List<IdentityUserRole<string>> _userRoles;
public SeedUsersRoles() {_roles = GetRoles();_users = GetUsers();_userRoles = GetUserRoles(_users, _roles);}
public List<IdentityRole> Roles { get { return _roles; } }public List<IdentityUser> Users { get { return _users; } }public List<IdentityUserRole<string>> UserRoles { get { return _userRoles; } }private List<IdentityRole> GetRoles() {// Seed Rolesvar adminRole = new IdentityRole("Admin");adminRole.NormalizedName = adminRole.Name!.ToUpper();var memberRole = new IdentityRole("Member");memberRole.NormalizedName = memberRole.Name!.ToUpper();List<IdentityRole> roles = new List<IdentityRole>() {adminRole,memberRole};return roles;}private List<IdentityUser> GetUsers() {string pwd = "P@$$w0rd";var passwordHasher = new PasswordHasher<IdentityUser>();// Seed Usersvar adminUser = new IdentityUser {UserName = "aa@aa.aa",Email = "aa@aa.aa",EmailConfirmed = true,};adminUser.NormalizedUserName = adminUser.UserName.ToUpper();adminUser.NormalizedEmail = adminUser.Email.ToUpper();adminUser.PasswordHash = passwordHasher.HashPassword(adminUser, pwd);var memberUser = new IdentityUser {UserName = "mm@mm.mm",Email = "mm@mm.mm",EmailConfirmed = true,};memberUser.NormalizedUserName = memberUser.UserName.ToUpper();memberUser.NormalizedEmail = memberUser.Email.ToUpper();memberUser.PasswordHash = passwordHasher.HashPassword(memberUser, pwd);List<IdentityUser> users = new List<IdentityUser>() {adminUser,memberUser,};return users;}private List<IdentityUserRole<string>> GetUserRoles(List<IdentityUser> users, List<IdentityRole> roles) {// Seed UserRolesList<IdentityUserRole<string>> userRoles = new List<IdentityUserRole<string>>();userRoles.Add(new IdentityUserRole<string> {UserId = users[0].Id,RoleId = roles.First(q => q.Name == "Admin").Id});userRoles.Add(new IdentityUserRole<string> {UserId = users[1].Id,RoleId = roles.First(q => q.Name == "Member").Id});return userRoles;}}
protected override void OnModelCreating(ModelBuilder builder) {base.OnModelCreating(builder);// Use seed method hereSeedUsersRoles seedUsersRoles = new();builder.Entity<IdentityRole>().HasData(seedUsersRoles.Roles);builder.Entity<IdentityUser>().HasData(seedUsersRoles.Users);builder.Entity<IdentityUserRole<string>>().HasData(seedUsersRoles.UserRoles);}
builder.Services.AddIdentity<IdentityUser, IdentityRole>(options => {options.Stores.MaxLengthForKeys = 128;}).AddEntityFrameworkStores<ApplicationDbContext>().AddRoles<IdentityRole>().AddDefaultUI().AddDefaultTokenProviders();
Password | Role | |
---|---|---|
aa@aa.aa | P@$$w0rd | Admin |
mm@mm.mm | P@$$w0rd | Member |
No comments:
Post a Comment