List all users from asp net core identity database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part 84 of a spirited Korra tutorial in this video we'll discuss how to retrieve all the registered users of our application from the identity database and display them on a view using the asp.net core identity API our list users view should look like the following if you have been following along with this course in our previous videos in this series we discussed how to retrieve the list of all roles and display them as you can see it right here at the moment when we navigate to slash administration slash lists roles this is the list of all roles similarly when we navigate to slash administration slash list users we want to see the list of all users what we're going to do now is going to be very similar to what we have already done retrieving and displaying these roles so in the interest of time instead of making you watch everything that I type I'm going to be pasting code and walk you through the important bits as usual I will include the source code text version of this video and slides on my blog and include the relevant links in the description of this video so you can copy and paste the code if required now the first thing that we want to do is include list users action method within the administration controller so let's flip over visual studio our administration controller is right here list users action method is going to be very similar to this create role action method so let's make a copy of this and then change the name of the method here to list users we want this method to retrieve the list of all registered users of our application the registered users are in this asp.net core identity database table a spinet users so if we view the data in this table at the moment we've got four users to retrieve these users we are going to make use of the belt in asp.net core user manager service user manager service is already injected into our administration controller so to retrieve the list of all users on the user manager service we've got users property which is going to return us the list of all users let's store it in a called users and then let's pass this variable to the view our obvious next step is to include list users view in the administration subfolder in the views folder we want to add a razor view and let's name a rays of view list users now if we take a look at the users variable it contains iqueryable of application user which is what we are passing to the view so for our list users view the model is going to be I enumerable of application user because I queryable implements i enumerable next we want to set the page title to all users and I want to include a conditional check here so if model dot any so if there are any users in the model then we want to display them on our view as you can see right here else we want a bootstrap for card with a card body and card header inside the card header we want to display this text no users create the add in the card body we want car title but this text used the button below to create a user and then a button to actually create a new user the code to create a new user is present within the distraction of our account controller so when this button is clicked we are redirecting the user if there are users in a model that is passed to the view then we want to display the list of all users each user details are being displayed using a bootstrap four card as you can see right here in the card header we're displaying user ID user ID is non editable so we keep it disabled in the card body we're just displaying user name but you can also display other user fields like email CD etc if you want to and in the card footer we have edit and delete buttons we'll implement these two buttons in our upcoming videos and then finally at the top we have this add new user button which allows us to create a new user if we want to in the if block let me paste some code this anchor element here is for the add new user button when I click this button we want to send a user to the register action of our account controller because that's where we have the code to add a new user and then we are using the 4-h loop to loop through each user in the model and as we are looping through we are displaying the user ID in the card header user name in the card body and in the card Fuda we have edit and delete buttons will implement editing and deleting users in our upcoming videos for now let's run this project and see what we have got so far at the moment we are on the home page let's navigate to slash administration slash list users there we go we see the list of all of our registered users as expected now when we click on this manage roles navigation menu item we send the user to slash administration slash list roles and with a list of all roles similarly we want manage users navigation menu item and we want these two menu items to be only displayed if the user is signed in and if that sign in user is in the admin role at the moment we are logged in using the username presume at primatech comm and we know this user is in the admin role so we are displaying manage roles and we want these two menu items manage roles and manage users to be displayed using a drop-down as you can see right here and when we click on this drop down navigation menu item we want to show these two options users and roles and here is the code required for that this code is again straightforward nothing new here using the asp.net core belt in a sign-in manager service to check if the user is signed in and if that signed in user is in the admin role if that's the case we have some HTML and bootstrap for styling classes to get this navigation menu item so for the drop down we are using this anchor element and all these classes that you see here are the bootstrap for styling classes so this anchor element provides us this drop down menu manage and that this div here with these two anchor elements provides us these two options users and roles when we click on the user's navigation menu item we want to send the user to the list users action with the administration controller and when we click on roles you want to send the user to list roles within the administration controller and we know this navigation menu is in our layout view so let's flip over to visual studio this is our layout view list and create navigation menu items are right here I'm going to replace this code block with a code block that we have just seen on the slide save our changes and take a look at the browser notice now in the navigation menu we have managed drop down and when we click on roles we see the list of roles similarly when we click on users we see the list of users now we've got a small issue here let me show what the issue is at the moment they'll log then using the username regiment prism techcom we know this user is in the admin role now let's click add new user notice we are redirected to slash account slash register let's register a new user using this email let's provide password and confirm password let's specify the city s London at the moment they're still logged in using this admin username presume at prism tech comm now when we click this register button this new user will be registered and we will be automatically logged in with this new user username look at this when I click register there we go we have our new user created and if we take a look at a spinet users database table you have a new user with the username XYZ that presumed comic reiated and if we take a look at our application notice we are automatically logged in with that new user username and we do not want this behavior if we are logged in using an admin username and when we create a new user we want to stay logged in as that admin user and the code to register a new user is within the account controller so let's flip over to account controller within visual studio here is the HTTP POST register action after we call create a sync method we are checking if that operation is successful meaning if the user is successfully created we are then signing in using that new user and we only want to do this if we are not already logged in so just about these two lines of code and to include another f-block so if we are already signed in and if that signed in user is in the admin role then redirect the user to the list users action within the administration controller if we are not signed in then these lines of code will be executed let's save our changes and take a look at the browser at the moment we are not logged in let's register a new user as an anonymous user let's use this email and provide the values for the rest of the fields as well if we click this register button now because we are not logged in this F block will be skipped and we will be automatically logged in using the new user username so let's click the register button notice they're automatically logged in using the new user username and this is fine in this flow because we are not already logged in as an admin user if we are logged in as an admin user this F condition will return true so we redirect the user to the list users action of the administration controller so the admin user can see the user that he has just created let's test this flow now let's log out and log back in as an admin we are logged in let's register a new user provide values for all the required fields notice our new user is created and we are still logged in as the same admin user in our upcoming videos well disk is implementing editing and deleting users that's it in this video thank you for listening [Music] [Music]
Info
Channel: kudvenkat
Views: 64,335
Rating: undefined out of 5
Keywords: asp.net core list all users, asp.net core list identityuser, asp.net core mvc list users, asp.net core list users view, asp.net core identity get all users, asp.net core get all users, asp.net core mvc get all users, asp.net core 2 get all users, asp.net core usermanager get all users, asp.net core get all users from aspnetusers, aspnetusers table get
Id: OMX0UiLpMSA
Channel Id: undefined
Length: 11min 12sec (672 seconds)
Published: Mon Jul 29 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.