Login System in TWO Lines of Code - ASP.NET Core 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is Felipe avilan and in this video we are going to learn how to make a logging system in aspironet core 8 with only two lines of code this is a new feature for web apis in espeonet core so let's get started first what I want to do is to create a new project is going to be an Espionage core web API project I'll click on next I'll call it logging into lines next I will use dotnet 8 preview because right now it is a preview version and I will use minimal apis although this will also work with controllers but I'll just use minimal apis so let's click on create now I need to install some nougat packages so I will go to the solution Explorer right click on our project management packages I'll go to browse and I will install three packages let's install first Microsoft Entity framework core SQL Server package so that we can use SQL Server I will use I will use Entity framework core 8 preview 6 which is the latest version as of now I'll click on install accept because I'm using visual studio I want to install the tools package so that I can use the Entity framework or commands on the package manager console so install accept and finally I want to install the identity Entity framework or package click on here install and accept alright we're good to go from here now I need to go to the Cs brush file because right now the application is not working with this inbound globalization active so I will just remove this from here and so we will not have any errors let me create a new class which is going to be the application DB context application DB context let's inherit from Identity DB context Ctrl dot to import this namespace control dot again now to generate this Constructor now we can go to the program class to configure Entity framework core this is just a setup for our application so Builder services at DB contest we're going to see the login system into lines of code in just a minute so application DB context options options use SQL server name default connection semicolon here let me put this in another line so that you can visualize everything better now let me copy this name let's go to the app settings.development.json file and let's say here connection with strings default connection and then server dot database let me write database directly logging into lines Integrated Security equal to true and Trust server certificate equal to True also alright so now we can go to the package manager console and say add migration initial and let's see that we will create the identity tables now let's go to the package manager console update database uh with this we're creating our database now let's create a login system in two lines of code so first we have to go to the program class and go to the part in which we are configuring the services here is fine and let me say Builder services at identity API endpoints identity user which is the class that represents an user in our application and then add Entity framework stores application DB context application DB context and that's it here now the second line of code is here we can put it here and we can say app map group identity map identity API identity user and believe it or not we have just configured a login system with only two lines of code let's test it so in order to test it I want to do the following I want to be able to use authorization here so that we can only use this endpoint point if we are logged in in our application and in order for us to use authorization logic we have to come back here and configure that so Builder services at authorization and that's it with this we're good to go control F5 to run our application let's go to Swagger and let's see that if we try to use this water forecast endpoint we are going to get back a 401 now let's see that as part as our configuration we have register logging and refresh register for registering a new user login for logging and refresh for refreshing the Json web talking so let's see that if we go to register we can put a username and password so let me say here Felipe and then let me say here this as my password and before I click on execute let's just see that here in SQL Server we can refresh and we can see that we have our database logging into lines tables let's see that in users right now we don't have anything we don't have any records it is empty now if I come back here and I click on execute we're going to see that after a few seconds we're going to get back a response a 200 okay and if we come back here we can click on F5 one more time and now we have Felipe here so as you can see we definitely register an user now let's come back here because now I want to get a hold of a Json web talking for that we have to log in so let's go to login try it out and we can come here we could use cookies if we want to but I want to use cookies I want to use ajson web talking so let me come here and when I say use a yes we're talking I mean I want to receive a yes or web token from this endpoint let me put here Felipe and the password execute and we're going to get token type better access token the access token expires in one hour and the I refresh talking I have to use the access token in order to be able to access these weather forecast endpoint that we have here and if I want to refresh the token we can use this refresh token that we have here we're going to see an example of that in a moment first we're going to make a test with this access token so what I want to do is that I want to copy this let me copy this and let's come to notepad because it is easier for me to copy it from here now let me come here let me copy this I want to use Postman for this test I can send Json web token through Swagger but just to save some time I'm going to use Postman so let's come to postman here we have a get request let me paste the URL you can see that again we got back a we got back a 405 because we made a mistake this shouldn't have been the identity logging endpoint but this one the weather forecast endpoint so let me just fix that right away send okay 401 now let's come back to here let me get the access talking copy and let's come to authorization better talking and here we paste the talking send and we get back a 200 okay so as you can see it works now what if we want to refresh the talking well for that we can use the refresh talking that we got when we log in so let me copy this and let's come back to Swagger we can do this from Swagger refresh try it out let me paste here the refresh talking execute and we're going to get back again and access token and a refresh talking I can copy this from here and come back to postman and I can replace this with the new access token and now I can press send and as you can see we have back A200 okay if you want to learn more about espiro net core buy my udemy course today I have courses on aspironet core and react spline core and angular and also I have a C sharp course that goes from zero to expert link with a discount to all of my courses in the description of this video thank you
Info
Channel: gavilanch3
Views: 18,400
Rating: undefined out of 5
Keywords:
Id: OOeg5O34r6Q
Channel Id: undefined
Length: 8min 25sec (505 seconds)
Published: Tue Aug 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.