Link to home
Start Free TrialLog in
Avatar of maqskywalker
maqskywalker

asked on

seeding data into ASP.Core Identity tables

I have a ASP.NET Core 3.1 MVC application using ASP Identity for Authentication and Authorization that I created using Visual Studio 2019

I'm using the Northwind SQL Server sample database.

So in my app I created a Migration for the ASP.NET Core Identity tables and then ran the Update-Database command which created these ASP.NET Core Identity tables in my database.

[Northwind].[dbo].[AspNetRoleClaims]
[Northwind].[dbo].[AspNetRoles]  
[Northwind].[dbo].[AspNetUserClaims]
[Northwind].[dbo].[AspNetUserLogins]  
[Northwind].[dbo].[AspNetUserRoles]  
[Northwind].[dbo].[AspNetUsers]
[Northwind].[dbo].[AspNetUserTokens]

I already know how to create new roles and users by using a form on one of my views.
Then on another view I am able to assign roles to a user.

Now I'm trying to learn how to seed users into the ASP.NET Core Identity tables mentioned above.

On the internet I've searched "asp.net core identity seeding data".

I found lots of good examples on seeding data into a regular table. I already know how to do that.

But I didn't really find a good example on seeding data users and roles into the ASP.NET Core Identity tables.


The difference I have seen is that when seeding data into ASP.NET Core Identity tables, I have to hash the password and other few columns.

Does anyone have a reference to a good article or video on seeding data into ASP.Core Identity tables in a ASP.NET Core 3.0 MVC or higher application?


ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of maqskywalker
maqskywalker

ASKER

Thanks. Your sample was very helpful.

In your example you created user with this line.

var defaultUser = new OblakSystemUser


I created a ASP.NET Core 5 MVC example where I seeded 2 users and it worked great

The first line of the two snippets for the 2 users looked like this:

var appUser1 = new AppUser {
// the user properties go here
}

var appUser2 = new AppUser {
// the user properties go here
}


If I needed to seed a hundred users would I just you a loop to create the AppUser objects instead of hard coding them like the sample above?


Yes. In my app, Oblak is my organization's name and we were creating an internal app and I had to customize the Identity User so, the different class name.

Also, the code snippet above is to create initial admin user. We allowed admin to upload an excel file to create rest of the users. However, as a one time setup step you can use a loop to create hundreds of users.
thanks for the great info.