Improved Auth & Identity in ASP.NET Core 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to PRP coding YouTube channel I am PR rind in this session I shall explain about the improvements in O and identity in asp.net corate so let's start I have created an asp.net core web API project and it is opened in my vs code editor if I click on this project here you can see that the target framework is set to net8 this project is configured to use Entity framework core in memory database for that that I have installed the packages microsoft. Entity framework core and microsoft. Entity framework core. inmemory then I have added the identity nuget package to manage users passwords profile data roles claims tokens email confirmation and more in the program.cs I have configured the identity DB context to use the inmemory database and it is added to the services collection then I have created a minimal APA endpoint with a route welcome and it is returning a string welcome to PRP coding okay so now let me go and configure the authentication and authorization service for our project for that I can say builder. services. add authentication now prior to net 8 there is no built-in support for token based authentication and authorization with asp.net core identity for that developers had to rely on third party n get packages like JWT Bearer net 8 now supports token based authentication and authorization with asp.net core identity for that we can use do add Bearer token and specify a authentication scheme here for that I can say identity constants dot bar SK now one very important point to note here is that this token is not a JWT token this is is a Microsoft generated token okay now let me go and configure the authorization service so for that I can say builder. services do add authorization Builder okay that's it so we have configured the authentication and authorization for our project okay so now we need to configure identity to use the Entity framework or database and expose the identity and points uh for that let me paste the code here and here you can see builder. services. add identity core and here I have specified the user as the identity user then do add Entity framework toes and here I have specified the database context as the identity DV context and then I have exposed the API end points by using the add API endpoints now we need to go and map the routes for the identity endpoints prior to net8 developers have to write code to map the routes for the identity and points and then use the built-in signin manager and user manager services for registration login forgot password change password and so on developers used to write a lot of code in net 8 mapping the routes for identity end points can be achieved by using a single line of code just add the code app. map identity API and specify the identity user that's it so this will go and add add end points for registration login forgot password change password and so on so everything is taken care by the asp.net core identity now the last thing that we need to do is to add the authorization for the endpoints okay for that let me add dot require authorization now let me go and run this application and test these endpoints I mean the welcome endpoint and the identity API endpoints via Postman the application is building and here you can see that it is listening in port number 5,000 now let me switch to postman and here you can see that I have created a postman collection named ime o with three request register welcome and login so let me click on the welcome and Point close this and this is a get method HTTP local 5000 welcome so let me click on the send button and here you can see that we are getting 40 F unauthorized that is because in program.cs in the map welcome endpoint we have added require authorization so it is expecting a token in the header okay for that let me go and register a user so let me click on the register end point this is a post method HTTP local 5000 register and if I click on the body here I have added email as PRP prpr training.com and password PRP training at12 345 now let me click on this send button so here you can see that the status is 200 okay now let me login and here this is a local host 5000 login and in the body here I have passed email as PRP prpr training.com password PRP training at 1 2 3 45 now let me click on this send button yes it is a 200 status okay request and here we got the token type as Bearer and we received an access token and if I scroll down here you can see that it got a expires in 3,600 and a refresh token as well now let me take this access token okay uh let me copy this access token and let me go to the welcome endpoint and in the authorization I will set the type as where token and paste the token here now let me click on the send button now and here you can see that welcome to PRP coding so everything is working as expected now if you look at the program.cs here you can see that I have added only one API endpoint with a route welcome but from the postman we are able to access the register endpoint as well as the login endpoint so from where these endpoints are coming from actually it is coming from this single line of code app. map identity API this single line of code has added the identity and points in our application one more thing I would like to demonstrate is that here I have used welcome to PRP coding which is a hardcoded message now I want welcome to whatever is the username that is registered in our application for example whoever is logging in that username should be printed over here so for that I can use the claims principal claims principle and here I can say uh let me use dollar symbol here and instead of Welcome to PRP coding I can say claims principal do identity. name okay so here let me make it as nullable okay now let me refresh this application okay and let me switch to the uh registration end point again since it is using uh inmemory database so all the data will be lost so let me try to do the registration once again let me click on the send okay now let me login let me click on the send here yes the login is successful now let me copy this uh access token copy that let me go to the welcome and in authorization be a token let me paste this token over here and let me click on the send button and this time you can see welcome to PRP prpr training.com so this is all about improvements to Au and identity in asp.net core 8 if you find this video helpful then please like share and subscribe to my YouTube channel and stay tuned for the next updates thank you
Info
Channel: PRP Coding
Views: 3,126
Rating: undefined out of 5
Keywords: .Net 8 features, .Net Developers, .net, .net 8, .net core, C#, C# 12 new features, C# tutorial, asp.net core, asp.net core 8, asp.net core identity, asp.net core minimal api, asp.net minimal api, c# programming, coding, core, development, dotnet, dotnet 8, dotnet api tutorial, dotnet core web api, dotnet minimal api, dotnet web api tutorial, ef core, entity framework c#, how to, identity endpoints, microsoft, minimal api, minimal api dotnet, programming, training, tutorial, vscode
Id: T8S4EuT5M5c
Channel Id: undefined
Length: 7min 59sec (479 seconds)
Published: Mon Jan 01 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.