Oynatıcıya atla
Ana içeriğe atla
Altbilgiye atla
Ara
Oturum açın
Tam ekran izle
Beğen
Yorumlar
Yer işareti ekle
Paylaş
Çalma Listesine ekle
Bildir
43 - Seed Users and Roles
Mustafa Demirhan
Takip Et
02.09.2024
Kategori
📚
Öğrenme
Döküm
Tüm video dökümünü görüntüle
00:00
Welcome back guys. Let's jump back over to
00:03
our API project and just go straight to our DB context.
00:07
What we're going to be doing here is seeding at least two roles,
00:12
and then at least two users,
00:13
one per role.
00:15
The seeding activity looks very similar to what we would have done,
00:20
when we were to put in some sample cars.
00:22
But I'm just going to add a new line below all of that.
00:28
We have modelBuilder.entity.
00:30
This time, our entity is going to be our identity role.
00:35
Let's start off with the role.
00:37
Then we say hasData just the same,
00:41
and we are doing two roles.
00:45
We are doing one for administrator and one for user.
00:50
For the administrator role,
00:53
and I'm just going to enter them so that you don't have to watch me
00:57
type too long or too much.
00:59
For the administrator role,
01:00
we have nameAdministratorNormalized,
01:02
name is Administrator in all caps.
01:06
For the ID, I put in that GUID myself.
01:10
With identity records,
01:12
they use GUIDs for their ID values.
01:15
You can easily find a GUID generator online and use that.
01:19
The reason for this is we need to have
01:22
this role ID set so that
01:25
the migration will know that this is the ID it should use,
01:29
one, and two for anything else that we're going to
01:31
be seeding that requires a reference to the role.
01:36
For instance, when we assign the user to the role,
01:38
which we're about to do,
01:39
then we know which ID matches.
01:43
You can go ahead, hit pause, replicate that.
01:46
You don't have to use my same GUID value.
01:48
You can go and find another one and use it there.
01:51
That should be easier for you to do.
01:53
Then we're going to have another section similar to this,
01:58
but for the users.
02:00
What I'm going to do firstly is create a hasher.
02:04
I'm going to say var hasher
02:07
because we want to hash the passwords.
02:08
Of course, we never store passwords as plain text in our databases.
02:14
We have the built-in library called PasswordHasher,
02:17
our built-in class called PasswordHasher,
02:20
that takes the user class type,
02:23
that should contextually guide the hashing activity,
02:28
and we're using the default identity user.
02:31
Then I'm going to generate two users.
02:34
That's what that looks like.
02:36
We have the same form factor model builder.entity relative to identity user.
02:43
Then we say that has data,
02:46
and then I'm putting in two users.
02:49
We have new identity user,
02:50
went and found a GUID,
02:52
use that, and I'm just using email address and username as the same values.
02:58
Email and username would be lowercase and normalized email,
03:01
normalized username are uppercase,
03:04
but they're all just the email address.
03:07
We hash the password.
03:10
We say password hash is equal to hasher.hashpassword.
03:13
We don't need that first parameter,
03:16
and we have that special password.
03:18
I always use this demo password because it meets
03:21
the minimum requirements of identity's password rules.
03:27
Then we go ahead and say the email is confirmed is true.
03:31
That technically is also optional,
03:34
but you can go ahead and do all of that for our identity users.
03:39
Then to wrap it all up now,
03:41
we just need to go ahead and assign the user to the role.
03:45
We're going to do this once more,
03:47
except this time we're doing model identity relative to the identity user,
03:55
model builder.entity, identity user role,
03:59
and then this would be relative to the type string.
04:03
Then inside of has data,
04:06
we're just going to create two records assigning
04:10
the admin user that we're seeding to the admin role that we're
04:14
seeding and the same for the user to the user role.
04:17
Then that is going to look something like this.
04:21
We're going to end up saying new identity user role relative to string,
04:26
the role ID.
04:28
This is the role ID for administrator,
04:32
and then the user ID for the admin user.
04:36
Then we do the same thing for the user and user role and user.
04:42
That's really it. Of course,
04:45
this makes our DB context bloat a bit,
04:47
but since the focal point isn't necessarily architecting this and making
04:53
it into a full-fledged separation of concerns and
04:56
solid principles and all of those things,
05:00
I'm not going to go in-depth into separating all of those things.
05:04
Now, I just wanted to get the fundamentals
05:06
and we're just going to do everything in one place.
05:08
It is minimal anyway.
05:09
It's a minimal project,
05:10
so I'm not trying to create too many files at any rate.
05:15
But once we've done all of that,
05:16
we can go back to our package manager console,
05:19
and then we can run another migration.
05:22
I'm going to say seeded,
05:24
the default roles and users.
05:30
Then we can wait. Of course,
05:33
we always make sure that the startup project is
05:35
the API project or at least a project that the migration needs to be running.
05:39
There is our migration.
05:43
It's inserting the default roles,
05:46
it's inserting the users,
05:49
and then it is inserting records that will associate the role and the user.
05:54
Then of course, to end all of that,
05:57
we say update database.
06:00
Once that is done,
06:02
we can call this activity completed successfully.
06:06
Now we have default roles,
06:07
users, and the assignments.
06:10
What we will do when we come back is continue with the hardening of the API.
06:14
What we're going to do is put in our JWT rules and configurations for our API,
06:22
as well as modify our login method to actually validate the user,
06:31
and then generate an actual token which we will respond with.
Önerilen
23:05
|
Sıradaki
47 - Adjust App Flow and Layout
Mustafa Demirhan
02.09.2024
0:56
38 - Section Overview
Mustafa Demirhan
24.08.2024
10:11
40 - Set Up Login Endpoint
Mustafa Demirhan
24.08.2024
1:21
37 - Section Summary
Mustafa Demirhan
24.08.2024
27:45
46 - Set Up Login Logic and Flow
Mustafa Demirhan
02.09.2024
2:16
49 - Section Summary
Mustafa Demirhan
02.09.2024
5:16
48 - Handle Logout
Mustafa Demirhan
02.09.2024
15:35
42 - Add App Login Page
Mustafa Demirhan
02.09.2024
6:51
25 - Section Summary
Mustafa Demirhan
24.07.2024
14:00
36 - Essential Fixes
Mustafa Demirhan
24.08.2024
20:55
21 - Implement a Details Page
Mustafa Demirhan
24.07.2024
6:41
26 - Section Overview
Mustafa Demirhan
24.07.2024
17:56
44 - Authentication and Authorization to API
Mustafa Demirhan
02.09.2024
10:31
17 - Set Up View Model and Command
Mustafa Demirhan
18.07.2024
14:11
33 - Refactor and Publish API To Local Server
Mustafa Demirhan
24.08.2024
8:41
41 - Add App Loading Page
Mustafa Demirhan
02.09.2024
0:56
11 - Section Overview
Mustafa Demirhan
18.07.2024
7:16
20 - Style Our List Page View
Mustafa Demirhan
18.07.2024
14:28
19 - Implement Data Template and Collection View
Mustafa Demirhan
18.07.2024
1:07
10 - Section Summary
Mustafa Demirhan
15.07.2024
5:03
08 - Shared Resources
Mustafa Demirhan
15.07.2024
23:31
24 - Setup Form to Add and Delete Records
Mustafa Demirhan
24.07.2024
4:46
13 - Create Data Model
Mustafa Demirhan
18.07.2024
1:32
01 - Introduction
Mustafa Demirhan
15.07.2024
16:25
02 - Setup Development Environment
Mustafa Demirhan
15.07.2024