Okay, now that we've set up our store context and we've got our user entity, let's head over to the
program class to continue configuring this and we'll open up the Program.cs.
So pretty straightforward to do.
We just need to add a service which is going to provide us with the identity API endpoints.
Those ones I was mentioning in the previous lesson that gives us things like the login, the register
and so on, and configure the Entity Framework stores, which is responsible for creating the identity
tables in our database.
So to do this we'll use builder dot services.
And we can use Add identity API endpoints.
And we pass in here as a type parameter the user that we created from API entities.
And then we give it some options.
And we'll open an arrow and then curly brackets so we can put an expression in here.
Now what we could do here is we've got options that we can configure various aspects of identity.
Things like the password complexity and things related to the user.
I'll just do one of these and let's say, for example, we don't want users to sign up under different
usernames that have different email addresses.
So we might specify that we want them to require a unique email.
Another thing that we might want to do is take a look at the password options.
And all of these different options require digit required length, require unique characters, require
lowercase, non-alphanumeric, and uppercase.
What this means is that by default, identity ASP.Net identity requires unique passwords, and the default
values for these is.
It has to be at least six characters in length.
It requires a digit or a number.
It requires unique characters.
So we can't just have a, a, a a, a as a password, and it requires a lowercase character and it requires
non-alphanumeric a special character as well as well as an uppercase character.
So in order to register a user with identity, then we have to meet the password complexity requirements.
I'm going to say that's fine.
I want complex passwords and I'll leave that as it is below the parentheses.
The closing parentheses.
We just need to add a few more bits of configuration.
We'll add roles.
We'll add them now, but we're not going to use them for quite some time.
And for the roles we'll just use the identity role entity that we get from ASP.Net identity.
Add the parentheses and we add one more bit of configuration to add identity framework stores.
And we pass this the store context.
And you'll see this when we create a migration, it's going to create us a bunch of tables related to
identity.
So below that in the HTTP request pipeline section our middleware we also need to enable middleware
for this as well.
Now above the controllers and ordering is especially important here as well.
So the first bit of middleware we add is App.use authentication.
And we use authentication so that our app knows who the user is by validating their identity.
So is it the user Bob that's authenticating or is it the admin.
And then below this we'll use the app use authorization.
And this is our application's way of defining what that user is allowed to do.
Is Bob allowed to access this area or is the admin allowed to access this area.
So we need both of those bits of middleware here.
And then another bit of middleware we need to add is for our API endpoints for identity.
And the way that we specify this is we'll say app map group and we'll say API.
And then add a period and then map identity API.
And once again we pass this our user entity here.
So this map group just means that when we use these we're going to need to specify API as part of the
URL.
So it's going to be API forward slash login for example.
And then the map identity API gives us effectively all of the endpoints I was highlighting here, which
is all of these.
So just by adding that we have access to all of that functionality.
So we'll also go back to our seed class as well because we'll create a couple of users inside our application.
So we'll open up the DB initializer class.
And we're actually going to add something else to this as well.
So as well as the store context we're going to need our user manager which we get from ASP.Net identity.
So as a second argument here I'll specify user manager.
And again we use our user entity that we've created for this and we'll call it user manager.
And after the database migrates let's check first of all to see if we have any users in our database.
And if we do not then we will seed a couple of users in.
So we use the Not operator.
We'll use our user manager and this gives us access to our users.
In fact it gives us access pretty much to our store context at this point.
But specifically here we're going to access the users.
But the user manager gives us extra functionality than the store context.
It's like having access to our store context and all of our user functionality at the same time.
So we'll check to see if we have any users in our database, and we'll use the any to check that.
So if there are no users then we're going to seed some users in.
So I'll create a new user and say if our user equals new user and we'll give it a username and we have
access to that property for the user and the username, in this case I'm going to specify.
And even though it's username I'm actually going to specify an email address here.
And I'll explain why soon when it comes to logging in.
And we'll also have an email which I am going to set to exactly the same thing.
Looks a bit weird this, but there is a reason I've specified username as Bob at test.com and not just
Bob, and I'll explain why that is soon.
For now we're just going to get things configured.
So below the var user let's use our user manager to create the user.
So I'm going to use await as this makes an or execute something against our database say await user
Manager.
And I'm going to use the create async method to create a user.
And if we take a look at the options for this, the second overload I've just come down to in the documentation,
this takes the user object which we've created up here.
And it also can take a password that we can supply so that when we create this user it has a password
we can use.
So I'll pass in the user and then create a complex password.
And please bear in mind this does need to be complex.
So I'm going to use capital P a dollar dollar W0RD.
And just a word of warning here.
If you do use a non complex password then this will fail when it comes to starting our application and
attempting to seed our data, and it's going to pretty much fail silently as well at this stage, unless
we increase the logging level of what's going on when we do this.
So please do make sure you've got a complex password here.
Otherwise your users will not be seeded into the database and it won't give you any good information
about why it went wrong.
I'm also going to take the opportunity to use the user manager to add to role async for this user.
So I'll specify user and I'm going to specify member as the name of the role.
Once again this is a string.
Please make sure it matches one of the roles that you've created in the store context.
And I'll just copy this code and paste it below, because we'll also have an admin user as well.
We'll create it at the same time, although we're not going to use it for quite some time.
So I'm just going to change Bob to admin and admin there as well.
Keep the password, but I'm going to add the admin to two roles.
So instead of add to roll async, I'm going to use the version of this that takes an array of roles.
So I'm going to wrap the member in square brackets, add a comma, and also specify admin.
And I've got an error inside here because I need to add the user manager to this as well.
Which means we need to get our user manager as well inside this method.
So I'm just going to copy this line for var context and paste this below.
And I'm going to call this user manager.
And I'm going to get hold of the user manager.
And once again we specify the user entity inside there.
And instead of saying fail to retrieve store context I'm going to say user manager.
And then as a second parameter to our seed data I'm just going to pass in the user manager.
So that's the setup.
And in the next lesson we'll take a look at creating a new migration and checking out what this gives
us.