ASP.NET Core MVC Login and Registration using Identity | Asp.net core tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Manoj and welcome back to my YouTube channel in today's video we will discuss and talk about asp.net identity so what is asp.net identity it basically it's a management where we have user registration page user login page and reset password so on and so forth so we don't have to write our own code and we don't need to create our own design for these Pages all right so let's open Visual Studio and see how we can do it so for this I'm using Wheel Studio 2022 create a new project and from here we will select asp.net core web app model view controller project so asp.net model view controller click on next so let's give name asp.net identity app click on next uncheck this configure to https so now this will create a brand new asp.net core web app application for us and we'll see what is the default file and folder structure in isp.net core web app so the project has been created and you see these are the default file and folders so in the controllers holder we have got a One controller home and in the views we have a home folder in that we have index view we have a privacy View and in the model we have error view model all right so now the first step is we have to install a package so right click on the project manage new get packages and go to browse and we have to type Microsoft Visual Studio code generation.design select this package and make sure you will choose the right version so I'm going to choose 6.00 and install so this will install your correct version as per your VL studio if you choose higher version then it will show you the compatibility issues so make sure you will choose 6.0.0 so I accept the package has been installed now let me close both of them save and now the next step is we have to add our identity so right click on the project go to add add scaffold item and click on identity identity click on ADD so it will display the identity options so from there we will choose account login account registration forget password so here let's use account login logout register and reset password you can choose whatever option you want but for the time being I'm choosing these four login logout register register reset password so here if you see here we have a default layout and now we have to choose another one so click on the three dots go to views go to shared and layout.cs HTML okay so this will set our default layout for all these pages that is step one now second step is we have to add rdb context class so click on ADD so you see by default it is showing you this name but I think that is this is a very long name so let's make it little short add and the same thing we have to add a user class so click on this plus sign so same let's change it sample user click on ADD now we have followed all the steps we have chosen the options we have set our layout.csstml we have created DB context class and also the user class so click on ADD so it will take a moment and it will create all the respective partial views for us like register forgot password registration user and all these controllers and this one user class and DB context class so the moment it will install all the things so here in this solution Explorer we will see a complete a new folder called area and inside that we have an identity and we have all our DB context and user context class you see the scarf folding has been done and as I have already mentioned we have a new area folder so let's expand this so we have an identity folder and inside this we have two folders data and pages so let's expand data first so here we have our DB context class all right and we have our sample user class perfect and in the pages we have all our partial views login logout register reset password and other phone all right so we are done now so let's open this register see this is our resistor partial View and this is our login view all right so now let's build the solution and try to run it and see what we have got well succeeded so debug start without debugging so you see we have a very basic asp.net core web API interface so here we can see there is no login and registration link all right while links are not there so for that we have to do some changes so we have to go to our default layout which is in shared folder layout.cs HTML and after the snap we have to add our partial view so partial and we have to choose our login name if you see over here we have got underscore login partial.cs HTML all right so we have to pass this login partial save refresh I think we have to build this so Ctrl shift B well succeeded let's refresh now so see we have register and login all right so if I click on register we see nothing is happening right so the reason is asp.net identity is implemented in a way for razor pages so this is our asp.net MVC application so we have to inject our asp.net razor pages so how we can do that so go to program.cs file and inside here we have to add our app Dot map Razer pages so we have to give our razor Pages mapping over here so save now again let's build the project Ctrl shift B done and let's refresh it oops let me remove this and let's refresh now click on register you see we have got our registration page perfect so that is one and if I click on login so we are on login page so both are working now if I go back to register so here we by default we got three things email password and confirm password all right but if you want to add another two property let's say your name your mobile number your address how we can do that so for that let's go back to visual studio and in the sample user which is our model and this is inherited from entity identity user so here first we have to add some properties whatever property you want for example I am going to add property like first name and let's add another property property public string last name so this is step one and now we have to add these properties in the DB context class because this class is responsible to save these properties in the database all right so in this on model creating override method so here we have to add our class so before that let's create another class so public class application user entity configuration and this will inherit our I entity type configuration and TT and this will take our user class which is our sample user sample user to ser so if you see this is an interface so we have to have Implement all the methods so click on it and Implement interface so we have implemented our interface now here we have to inject this class so Builder Dot apply configuration new and this class name done now here we have to add both the properties so Builder dot property tax Dot first name let's say you want to specify the max length so has max length let's say 100 and same thing do it for last name last name has max length 100 save so we are done with rdb context class now let's open package manager console and now we have to add our migration so add migration and let's say initial migration so basically what it does it will create a script which will be going into database to create a complete database with this table that user all right so let's wait for a while you see we have got the complete script from here perfect now let's open Package manual console again and now we have to type update database but before that let's go to our app settings.json and here you have to change this server name so let's open SQL Server so by default you will get this kind of server name when you add DB context class but you have to add your own server name which you can get from your SQL Server so this is your server name let's copy and replace your existing server name with this save and now just execute this command update database so this will execute our database and we can able to see the database in SQL Server so let's quickly open this SQL Server also you can see it is executing the script done perfect now let's go to SQL Server and if you see the database name this is the database name and in the databases let's expand this okay perfect this is our database let's expand it and in the tables as you can see these are the default tables which we got created when we run our update database script so see this is the isp.net users so let's open a new query and select star from sp.net users execute so you see we have got both the properties whatever we added additionally first name last name these are the default columns you can say the fields which we get on our identity application all right perfect now go back visual studio and let's try to run it debug debug without so click on register so from here you can register yourself you can log in yourself all right so this is about today's video and this is the part one in the second part of this video I'll show you how we can add these first team last name over here and whatever changes are there we so that we can hold our value of first name elastium and store here in this table all right so this is about today's video I hope you like it if you did so hit the like button share comment and if you haven't subscribed my Channel Please Subscribe and I need your help and support all right thanks for watching take care bye
Info
Channel: Manoj Kumar
Views: 30,263
Rating: undefined out of 5
Keywords: asp.net core mvc login with Identity, add asp.net core identity to existing project, asp.net core customize identity, user authentication in asp.net core mvc, login and logout in asp.net mvc, .net core mvc user authentication, asp.net core identity step by step, asp.net core identity database schema, asp.net core identity database script, asp.net core identity example, asp.net core identity from scratch, asp.net core identity framework, asp.net core identity getting started
Id: 9lRva0L12wI
Channel Id: undefined
Length: 15min 32sec (932 seconds)
Published: Wed Mar 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.