• last year
Transcript
00:00Welcome back guys. Let's jump back over to
00:03our API project and just go straight to our DB context.
00:07What we're going to be doing here is seeding at least two roles,
00:12and then at least two users,
00:13one per role.
00:15The seeding activity looks very similar to what we would have done,
00:20when we were to put in some sample cars.
00:22But I'm just going to add a new line below all of that.
00:28We have modelBuilder.entity.
00:30This time, our entity is going to be our identity role.
00:35Let's start off with the role.
00:37Then we say hasData just the same,
00:41and we are doing two roles.
00:45We are doing one for administrator and one for user.
00:50For the administrator role,
00:53and I'm just going to enter them so that you don't have to watch me
00:57type too long or too much.
00:59For the administrator role,
01:00we have nameAdministratorNormalized,
01:02name is Administrator in all caps.
01:06For the ID, I put in that GUID myself.
01:10With identity records,
01:12they use GUIDs for their ID values.
01:15You can easily find a GUID generator online and use that.
01:19The reason for this is we need to have
01:22this role ID set so that
01:25the migration will know that this is the ID it should use,
01:29one, and two for anything else that we're going to
01:31be seeding that requires a reference to the role.
01:36For instance, when we assign the user to the role,
01:38which we're about to do,
01:39then we know which ID matches.
01:43You can go ahead, hit pause, replicate that.
01:46You don't have to use my same GUID value.
01:48You can go and find another one and use it there.
01:51That should be easier for you to do.
01:53Then we're going to have another section similar to this,
01:58but for the users.
02:00What I'm going to do firstly is create a hasher.
02:04I'm going to say var hasher
02:07because we want to hash the passwords.
02:08Of course, we never store passwords as plain text in our databases.
02:14We have the built-in library called PasswordHasher,
02:17our built-in class called PasswordHasher,
02:20that takes the user class type,
02:23that should contextually guide the hashing activity,
02:28and we're using the default identity user.
02:31Then I'm going to generate two users.
02:34That's what that looks like.
02:36We have the same form factor model builder.entity relative to identity user.
02:43Then we say that has data,
02:46and then I'm putting in two users.
02:49We have new identity user,
02:50went and found a GUID,
02:52use that, and I'm just using email address and username as the same values.
02:58Email and username would be lowercase and normalized email,
03:01normalized username are uppercase,
03:04but they're all just the email address.
03:07We hash the password.
03:10We say password hash is equal to hasher.hashpassword.
03:13We don't need that first parameter,
03:16and we have that special password.
03:18I always use this demo password because it meets
03:21the minimum requirements of identity's password rules.
03:27Then we go ahead and say the email is confirmed is true.
03:31That technically is also optional,
03:34but you can go ahead and do all of that for our identity users.
03:39Then to wrap it all up now,
03:41we just need to go ahead and assign the user to the role.
03:45We're going to do this once more,
03:47except this time we're doing model identity relative to the identity user,
03:55model builder.entity, identity user role,
03:59and then this would be relative to the type string.
04:03Then inside of has data,
04:06we're just going to create two records assigning
04:10the admin user that we're seeding to the admin role that we're
04:14seeding and the same for the user to the user role.
04:17Then that is going to look something like this.
04:21We're going to end up saying new identity user role relative to string,
04:26the role ID.
04:28This is the role ID for administrator,
04:32and then the user ID for the admin user.
04:36Then we do the same thing for the user and user role and user.
04:42That's really it. Of course,
04:45this makes our DB context bloat a bit,
04:47but since the focal point isn't necessarily architecting this and making
04:53it into a full-fledged separation of concerns and
04:56solid principles and all of those things,
05:00I'm not going to go in-depth into separating all of those things.
05:04Now, I just wanted to get the fundamentals
05:06and we're just going to do everything in one place.
05:08It is minimal anyway.
05:09It's a minimal project,
05:10so I'm not trying to create too many files at any rate.
05:15But once we've done all of that,
05:16we can go back to our package manager console,
05:19and then we can run another migration.
05:22I'm going to say seeded,
05:24the default roles and users.
05:30Then we can wait. Of course,
05:33we always make sure that the startup project is
05:35the API project or at least a project that the migration needs to be running.
05:39There is our migration.
05:43It's inserting the default roles,
05:46it's inserting the users,
05:49and then it is inserting records that will associate the role and the user.
05:54Then of course, to end all of that,
05:57we say update database.
06:00Once that is done,
06:02we can call this activity completed successfully.
06:06Now we have default roles,
06:07users, and the assignments.
06:10What we will do when we come back is continue with the hardening of the API.
06:14What we're going to do is put in our JWT rules and configurations for our API,
06:22as well as modify our login method to actually validate the user,
06:31and then generate an actual token which we will respond with.