Session - 23 : TIMS - Authorization using ASP.NET Core MVC

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay hi everyone welcome to another session so in this session basically we are going to implement the authorization to our project in the last session we just saw how to implement authentication okay authentication means uh if you provide your username and password here and then let me do that if you provide your username and password click on the login then basically whatever user is login we are just showing his name here it means we have success sucessfully authenticated the authenticated the credentials and if that is valid we paste the data from the database we store that data into the inmemory cookies uh by using the identity provider of the Microsoft and added some claims into it claims means the information about the currently logged in user and also read that claims in order to show that data into the browser you can see I have added Sagar patil here as well as here it means uh we just P the information from the claims and display it here so every user whoever is going to log in will have its own name its own user ID everything so accordingly according to the current session current identity we will just show the dynamic name here and here now if I log out okay so once I log out and trying to access the dashboard page again it should not be accessible because you can see there is no name here here also no name it means in order to access this page you should be able to login to your account first then only this page should be accessible if someone tries to access this page without login we should show them uh unauthorized error 402 unauthorized otherwise we should at least redirect them to the login page again in order to see the information about this page now to do that we we have to Implement authorization in by using the net Cod so how to implement authorization let's take a look okay so as you know we are having this program. CS file in this file We have basically added the authentication related things okay this is middleware provided by one of the nugged package of Microsoft which we just added in last session here uh we need to add one more middleware for authorization okay so that middleware will be will be look like this okay this is for authentication and this is for authorization so authorization policies here we can add the policy like a builder dot Services add authorization this is for authentication I will add comment and this is for authorization okay and we need to add some policies so this is for admin only policy this is SP user only policy okay and if your currently logged in user has a role admin then this policy will get app uh applicable if currently logged in user has a role user this policy will get applicable okay so this is the middleware we have just added and now we wanted to add one more thing that is use authentication I think that is already available I noticed that yeah this one use authorization use use authentication use authorization that's it now what you can do there is a authorized attribute which you will have to add uh above the dashboard action method so if you go to your account controller sorry admin controller in the dashboard action method you can use authorized attribute like this okay so those user having admin role should be able to access this dashboard okay now this is for dashboard now let's do the hard deploy and run our project in the browser okay so if I add username admin password click on the login you can see we have successfully open the dashboard page okay if I click here log out and then if I try to access the dashboard page without login now it has redirect you to the dashboard page sorry uh login page with some error we will see why this CSS file is not loaded here let me check um inspect errors what is the error here it is trying to access the assets from the account okay so let me check the login layout in the login layout I suspect we forgot to add til symbol right here we forgot to add til symbol so that we need to add like this so colon til for slash assets forward slash and this replace all if you do this 15 occurr over there now let's refresh this page you will see the login page before that you need to do the hot deploy you can see login page is there so return URL is admin SL dashboard it means you got redirected from the dashboard page because of the authorization attribute now if I just login again okay admin 1 2 3 click on the login you will be able to see the dash board page but if I log out here okay and then I'm trying to access that dashboard page without login you will not able to see that page you will redirect to the login page again so this is authorization technique in net core how to do that quick recap first of all go to the front end project go to the program.cs file add this middleware okay authorization add authorization add some policies admin and user now go to your admin controller in the admin controller I have added one attribute here above right above the dashboard action method okay this page should only be accessible when someone login with the admin role if I add this attribute here then you cannot access this page without login this is the meaning of this attribute authorized attribute and this is the program.cs file where you have added one more line that is use authorization you have added the middleware you have started using that middleware if this rule has in place then in the controller whatever action methods are having this attribute will not be accessible without login that is called as authorization I guess we have cover it everything for this authorization I will show you demo one more time this is you can access only when if you log in as a Sagar patil or any other user with the admin role if I log out and try to access that page directly through the URL you will land up on the login page only you cannot see that dashport page without login that's the beauty of the authorization technique in net code let me me stop the recording
Info
Channel: Code With Bapu
Views: 102
Rating: undefined out of 5
Keywords: programming, c programming language, c# (programming language), c programming, what is c sharp programming language, programming project ideas, c# programming, c language, c programming projects, c sharp, java programming language, c sharp tutorial, top programming language 2022, best programming language, how to learn programming, programming project, introduction to c# programming language, how to learn c# programming language, uses of c# programming language
Id: FCNgUdBov8I
Channel Id: undefined
Length: 8min 31sec (511 seconds)
Published: Sat Oct 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.