ASP.NET Web API Identity in 5 minutes (user register, login ..)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the identity package adds the possibility to integrate authentication and authorization mechanisms into the web application it provides ready-made components for user enrollment managements which then allows the developers to focus on the features that they really need to add however until now features like user registration user login or password research were supported fully only through the rendered Razer web pages which then you could also use in the MVC applications but in the latest upcoming version of.net which is dot at 8 Microsoft also added the support for the web API applications in the form of some simple endpoints that you can use in this video we'll see how such configuration can be added to some web API project and also will see some features that are provided by the identity package to web API applications [Music] demonstration I'll just create a new empty web API application let's just call it identity.web API I will obviously select the.net 8 which is in preview and also I will not select the use control option and this will result in some simple endpoint added to the template but with those settings I'll just click create after a while our web API project has been created and is open in our Visual Studio solution so first of all maybe let me just get rid of the endpoint that has been added by default so the weather forecast endpoint as well as the weather forecast record in this way we have pretty clean web API so first of all I'll start by adding some packages that are required for the identity to work properly with Entity framework core so I'll just right click on the web project in the visual studio then click manage nougat packages in the browse section I will just search for the package code microsoft.asp.net core dot identity dot Entity framework core and having the option include previous set now I will be able to actually install packages which are not yet fully released versions and the latest preview package is the one that I'm going to install in the same way I'll also install another additional package which is in this case a Microsoft anti-framer core in memory package obviously I could also add some SQL database provider or postgrad provider but in this simple example I don't want to bother you with the need of adding the migration and then applying the migration to the real database so in memory database will do just fine so let me just install also the latest pre version of this package and having those two installed now we can go to the programs yes and set up the configuration needed for the web API identity so first of all I will just add the app.map identity API for identity user which is a public class in the Microsoft asp.core.identity namespace if I wanted to actually set up my own type of user to the identity obviously here's the place that I would need to specify the type of my user but in this example let's just go with identity user and if I would open the definition of this method as you can see it's nothing but just a batch of endpoints like register login refresh and so on which will be added to our application in form of minimal API but most of them are using the features from the identity Library like assignment manager or user manager to somehow manage the user check his password or register the user so in order for this to properly work now we have to set up the identity package and in order to do that first of all we'll have to Define our own type of the identity DB context so let me just paste here on the very bottom of this file a new class called appdb context which inherits from the identitydb context class or the identity user and as you can see there's also a public Constructor which only function is to pass down the options to the base class all right so having this class defined in our project now we can go ahead and add it to the dependence injection container by just invoking the add DB context method which takes our type of our DB context as a first generic argument and then in the options by using the in-memory database we can Define any name for the database which will be held in memory okay so having this defined now we can go ahead and add some configuration which is needed for the identity package so first of all as you can see I just invoked add identity core or the identity user and also I invoked add Entity framework source which takes our application DB context as its generic parameter the last method will be the add API endpoints which is also needed right now the last option that we want to add to our project is the authentication and authorization Services needed to properly log in and verify our application users so I will also add those two lines which is ADD authentication with the better token options and also add authorization Builder and maybe let's just add one simple endpoint that will require authorization let's call it test and as the result from this endpoint I'll just return the name of the claims principle identity from this endpoint so with this configuration I'll just open up the project and in Swagger we'll see what kind of endpoints have been added to our web API application and as you can see there is a batch of them and first of all we have the action to release it the user we can also log in and refer the user's token then you can confirm the user email reset his password or set up the two-factor Authentication also we'll have two endpoints to get the information about the current login user or to change his username or email so maybe let me just open the releaser endpoint and as you can see we'll have to type in three parameters username password and email so I just pasted some sample username password in an email and by clicking the execute Button as you can see we got the 200 okay response so right now if I open the login method here we have some additional parameters which will define whether we want to use cookies or not and as we need a better token I will not set up the cookie mode to true so I'll just go ahead to the body of this request and paste the same credentials so that same username and password and with this request body I'll just click execute and in the response from this request we have a few different information first of all we have the type of the token which is better then we have the access token which we can use to actually authenticate to the endpoints that require authentication then we have the information about the time in which the token expires and also the refresh token which can be used to refresh the token before expiring so maybe I'll just copy the access token that has been returned from this login method and let's go ahead and try to test our test endpoint which requires Authentication obviously we got 401 unauthorized because in any way we have not set up the authorization either in the request with the user's access token and to be able to properly test this authorization mechanism I will go to postman and here I'll just execute the same endpoint which is just test and again without the authorization token you got a 401 unauthorized so now let's just set up the better token authorization and paste the token that I have copied from the login endpoint response and if I click Send again now you see that we get 200 okay and in the response of the body and in the response body we go the hello test which and test is of course our username maybe let's also try the built-in account slash in for getting points and by executing this endpoint we'll get the claims collection of the login user so as you can see we got the ID of the logged user we got his username and his email so in this way by just adding few lines of codes we were able to actually add the identity package to our web API type of project
Info
Channel: Fullstack Dev
Views: 9,961
Rating: undefined out of 5
Keywords: c#, .net, .net core, c sharp, from scratch, visual studio, .net cli, cli, tutorial, free, coding, programming, web api, asp.net, identity, user management, user registration, user login, jwt token, bearer
Id: Z7nfi8CVGSQ
Channel Id: undefined
Length: 8min 17sec (497 seconds)
Published: Fri Aug 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.