ASP.NET Core 8 WEB API Authentication With Identity & Bearer Token | ASP .NET Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends so today I'm going to show you how to use Microsoft identity inet 8 to generate be tokens so these tokens can be used for authentication and authorization so I'll be creating a net web API uh and then I will create or write the code which gener this P token and then I'll show you how you can use Postman to call this web API and uh do the authentication and authorization right so uh so this is not JW token but this is the Microsoft identity Bearer tokens okay it uses the O2 protocol and it is highly secured encrypted uh and very popular so so it and it is uh supported by as.net web API and as.net platform so let's go ahead and see uh how to do this before we move forward please subscribe to my channel and don't forget to hit the Bell icon and of course like the video open Visual Studio 2022 and then click on create a new project here search for uh as. net core web API and click on this click on next button here uh give a name to your we web API be API okay so this is the name of our uh API and then click on next button so I have selected here by default is net 8 you can uh you know install net 8 and then use this uh if you're practicing uh if you want to see how to install net 8 I have a video on that I will provide the link in the description below so you can follow that install. net8 and then uh you know use that for your projects then click on create button okay so the web API project has been created and you can see the default weather forecast controller we will reuse the same controller and uh add the authorization and authentication to this okay so uh first let's start with by creating a folder so right click your uh project and create a new folder name this folder dat layer close this so data layer folder is created now and now let's add some uh net packages which you will need so right click on the project go to manage n get packages go to browse and here add identity dot Entity framework so this is the one so click on install accept okay next one you should install is the design so entity framework. design so Entity framework do Design This one click on install and the next one you will need is SQL Server okay so this one microsoft. framework code. SQL Server so install this as well so now you have all the uh new get packages so next thing that we need to do is the uh we will add some code right so now go back to your data layer folder and create create a class inside that so let's create a plus okay so dat contest identity is my class and then I will I will use identity DB context here and then so let's create this uh Constructor okay so data context uh entity Constructor here everything is fine so in order to generate it you just hit control dot okay then it will give you options and then you can just select that generate this Constructor so that generate the Constructor so now the Constructor and the class has been created so let's now go to the program.cs so here you have to add few lines of code the first uh so go here before this builder. build line okay so you can add the code here Builder dot Services dot add DB context and here you have to give data context identity okay which is our class which we created now so and then options ops. ug SQL Server okay so yeah you need SQL Server Express so so please install that okay when you uh if you don't have it so once you have SQ Server Express uh so this will work so use useq server here and then within this we need to write some more code we have to use the configuration so which will be Builder dot configuration dot get connection string yeah so let's name our connection string let's bring it to the next line here is semicolon and then let's uh name our connection string something like DB connection right so this is the this is the con string here and then the next line should be to add the entity API end points so which will be again Builder dot services do add identity API end points and here provide identity user class okay identity user so this one and then dot add Entity framework stores store course and the type should be your uh data context identity that we have used here so let's the type here now so we have added this two lines of code and the next thing you have to do is the uh add the authorization so dot add authorization uh Here app do map identity API and uh identity user again yeah so that's the type and then close it so okay so I think we have all the code that is needed here now let's go to the so let's copy this and we will create a connection string in the app settings so go here add here con strings and the string will be the one way have copied connection and here we have to type some code like server do SQL Express database equal to so keep some database name so whatever name you give here so in the same name your you know uh the a a a database will be created so I'm giving here like net o so then you have to say that you trust this connection so you have to add this parameter con equal to true and then also trest the server CER certificate okay TR server certificate as true so I guess that's it so it should should work now uh right now let's add a so since we want the authorization to be available to this controller we need to add a attribute here so so so I want anyone requesting this get here to get the weather data should be authorized otherwise they should get a authorization error so in order to do that I have to add a attribute here okay so which is the authorized attribute so authorize attribute added now go to package manager console here or you can go to tools uh and package manager console okay so open this and then check your Entity framework version first so to do that netf and here you should be able to see the entity framework. net command line uh and all these things here okay so l 8.0.3 so that is uh installed so which means we are good if not then you need to uh install that so to install uh the uh net Tool uh you have to type net tool install hyphen hyphen global net EF okay so there is the command and that will install uh this net command line tools now uh if I so I so I can hit that actually to show you what will happen but it will actually update it so yeah so see see the version has been updated uh to 8.0.4 so if it is not already installed so it will install it if it's already there so it will update it so now uh we can run the migrations so to run the migrations you need to write this net EF space actually net EF space migrations add and give some name here right any name is fine ABC I am saying in it so and then hit enter okay so the build has now succeeded and you should see this mtion folder me folder here and inside this you can see all this code so basically it will create all the roles Etc in the database table and then our authentication and authorization all these things will work it will be able to generate the be token everything now will work because of this code and the database corresponding database tables so that part is done now let's run it okay so everything is working fine okay I don't want to use Swagger uh to show it to you I will use post manra uh because it is very popular and a lot of people use it and it is also you can save your responses you know and uh requests and everything and then you can retry those it will be very easy for you so I'll be showing uh I'll be using Postman to show you how to call this API so open Postman now our postman has opened and you also need to keep this running right so so this Visual Studio keep it running so that web API is available in a particular URL so then next step is copy this okay up to this and the port will be different for you for every for different people it will be different so a random port number so and then paste this here and then here slash weather forast okay the focus so that is what so so this is the controller which we want to call from here so if you go here back to this so we are trying to call this get we're doing a get here so when I say SL wether forecast so this get will be called okay so but since there is authorized attribute there so it will fail right so if I run it so I should I will get a 401 on authorized because I have said that it needs to be the request needs to be authorized so when I am calling it it is looking for a token in the request so and it is not finding the token so it is throwing me this unauthorized error next step is to register user so go here and then type this uh so use the register uh text at the end of the URL and then use a post here you have to go to body and select raw and text as Json and here uh something like this you have to type okay give your email ID something some email email ID and a test know any any password it should have uppercase and number and a special character okay so let's now send this request to the API the API should come back yes with a success right so 200 okay means it is a success so the user has been registered now we can use the same user to login first right when you log in it will get a uh token in response and for all subsequent requests we have to use that token so in order to login then we have to see what should be the format of the login so you can go here to Swagger click here and uh here you can see that for login it is using something like this example right can copy this and it is a post request uh so post login and now we can copy this example like this so let's now go back to postman and uh for login again we will use the login URL so login go to body I can draw text as Json here so now we have to use this this thing so copy this go back here and paste it okay so rest of the things we can leave it as it is uh and then click on send will not work because it is a post so post and then again send so now we have receiv received a 200 okay and we have also got a uh beer token which is this this one right so copy this be token and go to your uh first tab right so which is this uh weather forecast here now we will call it again but using the be token now we are logged in right so we are we have we are logged in we have got a token so which means we should be authorized to access uh any page which has the authorized attribute we can access that so here click on authorization and here token type as be token and here you have to copy paste right you already copied so paste it here so now it is so I pasted that so so now it should work so click on send button and you can see in response we are getting all the data back from the API so this is how the be token Works hope this video has provided some value to you and uh again please subscribe to my channel hit the Bell icon and don't forget to like the video all right thanks for watching
Info
Channel: tutorpraveen
Views: 5,657
Rating: undefined out of 5
Keywords: .net, .net 8, dot net, web api, asp.net core web api, asp.net core web api identity, asp.net core, asp.net core identity, asp.net core 8 identity - authentication & authorization, asp.net core 8 identity, identity, c# tutorial, asp.net bearer token authentication, jwt, .net core, authentication in asp.net core web api, authentication, bearer token, bearer token authentication, bearer token web, jwt bearer token, identity management asp.net, dot net tutorial, .net programming
Id: c67Y2Q3rRi4
Channel Id: undefined
Length: 19min 42sec (1182 seconds)
Published: Fri Apr 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.