Creating roles in asp net core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part 78 of asp.net core tutorial in this video we'll discuss creating roles in a speed or net core using the identity API to create a new user using the identity API we use the built-in user manager class we discussed this class in detail in parts 65 66 and 67 of our asp.net core tutorial we also discussed how to extend this identity user class so we could store additional data about the user like their gender city country etc in part 77 just like how we have user manager class to manage users that is to create read update and delete users we have role manager class to manage roles that is create read update and delete roles they built-in identity user class represents a user and it is passed as a generic argument to the user manager class so both these classes can work together to manage users in the underlying database similarly identity roll applause represents a role in identity API and it is passed as a general argument to role manager so both these classes that is role manager and identity role can work together to manage roles in the underlying database like most things in asp.net core the role manager service is made available to any controller view or another custom class with no project by asp.net core dependency injection notice in this example right here role manager service is injected into the administration controller using constructor injection so a first natural step here is to add this administration controller to our project so let's flip over to visual studio we want to add a new controller so right click add we want to add a class and let's name this controller administration controller to make this class a controller we must inherit from the built-in controller class bring in the required namespace and then let's include a constructor so we can inject the role manager service to be able to inject role manager service we include a constructor parameter of type role manager bring in the required namespace and then we want to pass identity role as the general argument to role manager class so it knows it has to work along with this class to manage roles in the underlying database let's call the parameter role manager and then let's ask Visual Studio by pressing ctrl period to generate the required private field now if we take a look at secrets of our object Explorer except this employees table the rest of the tables here are generated by the identity API users are stored in ASP net users table similarly roles are stored in this SP net rules table so if we take a look at the data in this table we do not have any roles as expected when creating a new role we just need to provide a unique name for the troll the ID value is automatically computed to be able to create a new role we need a view model let's place that view model in the view models folder so right click add you want to add a class and let's name our view model create role view model we just need one property in this view model and that is for the role name the data type is string and the name of the property is role name this is a required property so let's include the required attribute bring in the required data annotations namespace next we need a pair of action methods within our administration controller one action method response to HTTP GET and the other one responds to HTTP POST let's name these actions create role first let's create the one that responds to HTTP GET this is a public method returns I action result and let's name the method create role we want this action method to respond to HTTP yet so let's decorate it with HTTP GET attribute and all this method is going to do is return the view the name of our action method is create role so asp.net core MVC is gonna look for a view with the same name three role in the administration subfolder in the views folder administration subfolder because the name of our controller is administration controller so to the views folder let's add a new folder name it administration we want to add our create rule view in this folder so right click on the folder add new item we want to add arrays of view let's name a raise of you create role the code in this view is going to be very similar to what we already have in this reaches to view so in the interest of time let's copy this HTML paste it within our create role view and then change the bits as required first of all the model for our create role view is this create role view model class so let's specify that as the model first and then change the page title to create new role and let's also change the h1 here remember to create a new role we need to provide a unique role name and in this create role view model class we have role name probably so we want to bind these three elements label input and span to the role name properly so let's change the ASP - for tag helper accordingly for all these three elements we don't need password confirm password and city elements so let's delete all of them and let's change the text on the button to create role with all these changes in place let's run our project and see what we have got so far we are on the home page to get to our create role view we need to navigate to slash administration slash create role slash administration because the name of the controller is administration controller and the name of the action is create role we need to be authenticated to get to the create role action within the administration controller that's because if we take a look at dot CS file notice here we have included a global authorization filter that requires users to be authenticated to get to controller actions unless the controller action methods are decorated with allow anonymous attribute that's how we are able to get the login and register actions without first being authenticated now let's login using the email presume at presume techcom there we go they see our create role view as expected at the moment the width of the button is set to 75 pixels in sight dot CSS so this create role button looks a bit awkward let's override the width by setting it explicitly in the create role view right here on the button let's set the width to auto notice now the button looks much better if I try to create a role without providing a name we get our required validation error if I provide a roll name and then click the Create role button we see a 4:04 error this is because at the moment within our administration controller we do not have create roll action that responds to HTTP POST so let's make a copy of this action method and then change the attribute to HTTP POST this create roll action method is going to receive create rule view model as an input parameter bring in the required namespace and let's call the parameter model if the model state is valid we want to create a new identity role object because remember in asp.net core a role is represented by this belt in identity role class populate the name property of this identity role object using the role name property of this incoming model object next we want to use this injected role manager service to create the role in the underlying data as you can see from the intellisense this method takes identity role object as a parameter and creates the corresponding role in the underlying database and this method returns tasks of identity result so first let's pass this identity role object as a parameter and we know this is going to return a task of identity result so let's create a variable of type identity result let's call the variable result as the name implies create a sync method is an asynchronous method so let's await it since we are calling an async method inside this create role action method let's also turn this method into an async method and wrap the result using a task we want to create the role in the underlying database only if the model state is valid so let's move this line inside the if block this result object has got succeeded property so if result dot succeeded so if this succeeded property has returned true then we know the role is successfully created in the underlying database so let's redirect the user to the index section of our home controller this result object has also got errors property so let's use a for each loop and loop over each identity error in result dot errors as we are looking through the errors let's add them to the model state object to add the errors to the model state object we have add model error method let's pass an empty string as the key and to get the error description on the error object we have description property if the model state is not valid then we know this statement is executed to this view method let's pass this incoming model object so if the model state is valid then we know the code inside this F block is executed else the create role view is rear-ended with the validation errors the user gets the opportunity to fix those validation errors and resubmit the view at this point let's save our changes and take one final look at the browser let's create a role with name admin there we go our new rule is created and we are redirected to the index section of our home controller at this point if we take a look at the a spinet rules table notice we have a role with name admin created the ID value is also automatically calculated which is a grid a globally unique identifier in our upcoming videos we'll discuss adding users to these roles and then also performing role based authorization that's it in this video thank you for listening [Music]
Info
Channel: kudvenkat
Views: 130,591
Rating: undefined out of 5
Keywords: asp.net core create roles, asp.net core identity create role, asp.net mvc core create roles, asp.net core create new roles, asp.net core create custom roles, asp.net core rolemanager example, asp.net core add new role, asp.net core add identity role, asp.net core identity custom role, asp.net core custom role
Id: TuJd2Ez9i3I
Channel Id: undefined
Length: 12min 20sec (740 seconds)
Published: Sun Jul 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.