Role Based Authorization in Asp.Net Core using C sharp

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today I'm going to show you how to use Microsoft identity core to assign roles to registered users or you can say um this is like role-based Authentication okay uh so let's begin I'm using visual studio 2022 and let's start a new project I'm using the model view controller okay let's Market uh okay I did it uh claims because I would be using the same example for uh recording a claims video I'm not using the https um it's okay but the browser will ask like uh to understand the application as a secure environment and other things so I'm just giving that bad okay let's take it as individual accounts now create I'll try pausing the video to save your time so okay guys the project has been created now um uh I'll go with a sequence so I have a sequence I'll Follow That steps okay let me check the app settings so this should be set to your connection string the connection default connection string here doesn't match mine so I'll just remove that and I'm going to uh get that for um this video so in the amp settings I added my connection string and I will use it as a different database uh save it now we'll make small changes in the program.cs file so if you're using an identity it will pick up the identity user and it will uh in the framework stores it will Mark application DB context as the table as the T okay now uh we need a few more things here dot add default token providers then dot add roles in between identity user okay so that's all in our program.cs if you want you can have a look at the below things like add map region pages is there map controller route is there um I pretty much app.run that's all we need in program.cs for um role management Let's cross that okay so let's go ahead and add the nugets we already have couple of uh nuggets installed when we created the project however uh those um those things are not up to date so we will have to up to date that one the latest version of whatever uh version you are using in your Dot and core project so I'm using.net6 so I will use the latest version of six let me just add them up and then I'll show you what all I added okay so these few things came up so I'll fix up with their latest versions and few more nuggets needs to be added so I'll add them up and I will show you what all I added okay foreign packages uh just to give you a brief uh select the project okay so this is what I did a couple of them came up when uh we created the project I upgraded them to the listed latest uh version and few more I added okay so this is basically all the uh nuget packages that I added okay let's move ahead um we will create a small uh um model class just to use it for um facing access to that page with our identity so model class add model I'll name it as products I'll copy the contents from another project okay so mod class products is ready save it okay let's go to the application DB context that enhance from Identity DB context and add it here sorry I missed the get set foreign will be created so okay um yeah so product is ready now uh I need to add a controller for that so just drink before that uh let's create our first migration let's name it as initial setup okay so I would show you uh this era uh can only be the role that drives from Identity role application to be context okay why did I miss anything let me check that sorry guys I did this identity user uh this is identity rules I'm sorry about that this is identity role Okay so moving on to the next uh we will create the um controller oh sorry it did not this one okay migrations I'm sorry okay so migrations run successfully now let's update the database my update database is going to create a database and then uh it will uh create the tables for us okay all the identity tables are being generated it's done now let's see claims track two you should have all the yes including the products table okay all right so this is done now the next step is to create the controller and then test it with some products using views with uh using Entity Framework products controller fine hmm products controller is now generated reverse this due to its products what did I do this is a oh I'm sorry I made it private okay sorry it's a bit it's public okay so uh products controller is created and the views have also been generated yes perfect now uh let's run it and see uh if we can create a product product didn't come here so what I missed is I missed adding it in the layout okay so I have to add it in the layout from the views from the shared layout copy the home products is there so let's see uh if I can create a product okay so I can give any ID a name should be like say TV description should be a television brand should be accurate circuit Samsung create one product is created create new suppose it's a washing machine brand let's skip exemption two products created okay let's create an ID so for example this one be let's say user two one ID is created okay the product controller let's mark it as authorized no Anonymous access only someone logs in they should be able to access it otherwise no one else could okay let's click on product it's asking for a login privacy I'm able to do that products and Sask for a login let's skip the user too I'm able to access now log out okay let's move ahead to the next step before going to the next step I would like to show you what all things we have got in the database so far uh I'm sorry this is different one in the database so far we have got one user one registered user and one uh two products okay so the remaining these three things are not used as of now Okay so okay all right next let's go ahead to the next step uh okay we'll have a user roles controller and add user roles to it let's set be an empty rolls controller okay it says home user rules I'll copy the contents from the different project okay so what well we did uh I got okay let's increase the font okay so I got a role manager and I got user manager then initiated both of them user manager most probably wouldn't be used in this video okay let's see uh index I'm passing a rolemanager DOT roles so this one will show me all the roles in the index page create will allow me to create some roles and uh this is the code that we are going to check if the role exists then it will move ahead if it does not then it will create a role let's create the views I have created an empty View and I will uh show you the views for create an index okay guys so this is the create View we are using the identity core and uh identity role is the model so uh that gives us a name for the identity role we will simply use the identity roles name and input the value for the name in the text box and submit it okay that's creation index will show us a h identity roles list of identity roles that exist in a tabular format okay let's run that and create some roles and then uh we will Design the registration page to add some roles in there sorry guys I missed the roles link here however I think I should be able to access rules from here I made some mistake what is that oh user rules I'm sorry add a role so let's say one is employee huh let's make it employees admin two roles created okay now we are going to add that role in the register option so uh anyone registering will have the role okay so uh let me add that in the layout that one is user rules and here it's rows okay these two things are done create index let's see in the database we got something yes we got two roles here now let's change the uh registration form to add roles here so now we do not have any pages so uh let's add scaffolded item and identity from here this one will give us a list of pages that we can use so we mostly would be using the uh register page from here however I'm fetching all of them just in case we might need them we don't really need them but we will be using the same project in future so I'm just getting them and we will modify them according to our use okay guys so identity has uh brought its pages so we will be using the register page before that I will need one more model class to make some modifications in the application user so I'll use a class and name it as application user and this one derives from Identity user okay let me add two column values to this identity application user so I'll just copy that Okay so two values we added first name and last name now we have to update the page from we have to update the database with this first name and last name using the migrations so again add migrations and app user okay I'm sorry I missed out one more step let's first of all use this thing in application to be context otherwise it would not show up okay now things are ready if it takes time I'll have to pause that sorry guys I'm just focusing on the length of the video so that I don't waste much of time okay so it brought up it brought it added two columns first name and last name and discriminator is for application user like it will Mark a name as this uh entry came from application user okay okay so first name and last name anyway I'll be using the application user any um in any uh page so it will put it as a note on the database that it came from application user and similarly we can use multiple uh um what you say identity pages and we can have different columns in them all right so let's uh that one is done let's see what we got so after this update we should have ah we didn't have did I not update the database okay now it created yeah we got this three more columns here okay discriminator first name and last name okay now let's modify the register page so that we can have our roles involved in it for that we will need uh again the rules manager not the user manager but the role manager okay got them I will uh copy certain things to add the first name and last name in a register code Okay so you can copy this thing and modify this one according to uh your requirement and Mark it as first name and last name okay so required display name and then public string first name copy the same thing last name gets it okay now one more thing for uh the roles require public string nullable role get set validate never it's not gonna validate this select list item uh the roles list so use the validate never when I enables like this item okay and then I'm going to add an input here and then in the on get async and then in the post tracing get is going to get you values for the form post is going to submit the values after you post the form first name and last name will have errors unless we fix the application user okay now one more thing after logger save the data to roles await user manager dot add role async user input.roll what type of role we assign to that user and this one that's coming up that will be fixed with uh the identity user mark it as application user okay so it's probably was uh uh not it did not have uh it was not using the models class so once it has added the models uh yeah this thing got fixed yeah yeah this one okay all right so what we did here we added two column values for displaying the name the first name and last name then uh we added a string value for the role that we will be assigning we added a select list item to get the roles list and then uh in the get async we initialize the roles list to have the text and the value to have all our roles uh that are saved and then in the post we added user.first name and last name below the email store dot set email async and then awaitusermanager dot add roles async user input.roll whatever role we are going to assign it to the user if the result succeeds okay then we fix the create user with application user as the type and activate a create instance application user and that's it save it now we are going to fix the register.cs HTML page the design page now uh we are going to add two more text fills text field for first name okay uh yeah if you want you can just copy the bottom one and uh change the dot emails to DOT first name and username to first name so one two three four four changes email username email email okay so this one will be first name okay now similarly the last name and one more for a drop down at the bottom before submitting okay now let's uh remove the dialogues next to the form this one the link that go Microsoft and just uh keeping out that div you can comment out but it's not required okay so we added the drop down just above the submit button and a first name and last name input DOT first name and last name we can copy it from the uh from the email a diff okay now it's all done let's go ahead run it and see okay the page has come hoping to see the rules here products is already okay products needs an authorization uh register let's register a few more users C first name last name roles should show us yeah okay so let's first add the admin this is the admin guy okay when admin is added now few users Market as an employee one more user so three employees and one admin all right so three employees and one admin okay one two one two three yeah okay let's um all right let's um give them access according to their roles okay so products controller has an authorized but it does not have any rules let's give them a role to employees user roles is accessible to everyone let's authorize it with admin okay this text should match this text from your roles this text should match with this one okay so user rules is secured for admin and products is secured with employees all right anyone else without these roles will not be able to access them okay let's see foreign an anonymous user I have access to home I have access to privacy roles I do not have access products I do not have access so only after login I'll get the access all right let's log in with uh uh an employee okay so I'm logged in as Usha let's see I'm able to access the privacy products I'm able to access edit delete yes I'm able to do all those things roles I'm not able to access okay access is denied because it's secured with admin roles now let's log out and log in as admin admin is logged in privacy able to access products is not able to access roles it's able to access create a role it's able to access okay so home it's able to access so this is roles based Authentication all right guys so uh that was all for this video um I hope you understood what we did if you want the to have a look at the codes um I can pause on some screens and you can note down the codes uh you can comment me where to post it I can post the codes for you so that will be a help for you to use the roles based authentication let me give you a few more tips so we can have multiple roles on a controller so let's say for uh products we can have employees and admin access on the products controller and we can also have individual access if I remove it from here and I can put them above above the the action results so for example if I authorize it for admin then employees won't be able to create a product only admin can create a product okay so that's how you can put in more authorization on the action results okay so this one will have only employees to create okay so if I put it like for admin only then it can have only admin like you can have employees and admin on the top and then give access only to the admin to create then employees can have access to other actions but not the create action okay so that's another feature all right guys that was all for this video um if you want to have a look at the products controller let me just make you go through it okay so you can have a look at what all things we did we have an index we have details to view this this one was uh generated actually this one was we generated that like with MVC views and uh uh wait I'll tell you what we did add controller yeah this one MVC controller with views using Entity Framework so products controller was generated user controller was created so we created this one to show the list of uh roles then to create some roles and then we used it in the register and the back end register.cs HTML okay if you want to have a look at it so you can have a look it's the first we got a role uh this one the read-only role manager uh variable then initialized with a Constructor then in the input model we added the first name and last name that we fetched from the application user then we added the roles uh the last before the confirm password required this one validate never the select list item initialize the select list item inside the gate async on gate async in the on post tracing added use it at first name and last name below the email store and add it away to user manager.ed roles I think below logger.log information changed this private create user from Identity user to application user inside the try block application user again and that's all and the changes in the register uh view page input.firstname input dot last name and the drop down above the submit button these were the things that we added and things went through all right guys that was all for this one stay tuned watch for the claims and that's very easy it will give you another level of access after you assign them roles so roles and claims will be a good value for you thank you thanks for watching have a good day
Info
Channel: Fired Developer
Views: 4,160
Rating: undefined out of 5
Keywords: Role based authentication in dotnet core, role bases in .net core, role based authorization in asp.net core, authentication in asp.net core, role based authentication in dotnet core 6, asp.net core identity, asp.net core identity role based authorization, ASP NET Core role based authorization, role based authorization in asp.net mvc, identity in dotnet core, identity in asp.net core, identity in asp.net core 6, asp.net core identity tutorial, identity in dotnet core tutorial
Id: zSNFK_A-Tu0
Channel Id: undefined
Length: 43min 7sec (2587 seconds)
Published: Tue May 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.