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?


ASP.NET* Entity Framework Core* ASP.NET CoreMicrosoft Visual Studio

Avatar of undefined
Last Comment
maqskywalker

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chinmay Patel

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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?


Chinmay Patel

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.
maqskywalker

ASKER
thanks for the great info.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck