Login and Registration using ASP.NET Core Web API and SQL Server | ASP.NET Core Web API tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is manoj welcome back to my youtube channel in today's video i'll show you how we can create login and registration web api using asp.net core first of all let's open sql server and let's create one more database so here let me create a new database so create database test toys f5 let me make font little bigger and let's create a new table registration and inside this let's take one id column that would be auto incremented and also make it primary key username where care password is active end and here email so let's select the command execute and now select star from registration so we have id username password email and is active perfect so we are done with our sorry we need to use this database and now we need to execute this table so now we have this table in test toys database perfect now open visual studio 2019 create a new project from the template you can select asp.net core web api project i have already created many projects so from here i'll get a direct link so same link you will get if you have already created like this asp.net core web api select on it click on next now we need to give the name so login registration app and let me give a location and then click on create so it will create our project so as you can see the project has been created and this is the default page of facebook.net core web api and if you see the solution explorer this is the default files and folder structure if i expand the controllers folder so we'll get a default controller weather forecast controller all right we have program.cs startup.cs app settings.json so let's close all of them close all tabs now first of all let's create a class so add a new folder first new folder give it a name models and right click on it and add a new class and give a name registration you can give any meaningful name so inside this we need some properties so for properties there is shortcut type prop and then hit tab so we'll use the same properties whatever we have created the fields in the table so another one is for username so again prop that is kind of string another one is for password and fourth one is for email and last one is for is active is active is integer type save again right click on this models folder and add a new class and give it a name response again you can give any meaningful name so inside this class we will have two properties one is for status code another one is for status message save now right click on this controller add controller and click on this api api empty click on add change the name i am giving registration controller click on add so the class has been the controller has been added now first of all go to appsettings.json so here we need to add our connection string so this is our connection string if you see this is the name of the connection this is the data source data source you will get from here so this is the server name is the data source and this initial catalog is your database name and if i go back to database engine so see i'm using windows authentication so for that i'm using this integrated security all right so we are done with our app settings go to registration so let's create the object of i configuration so public read only i configuration we need to add a namespace so control dot it will display all the related namespaces so we need this one double click so you see the namespace has been added and let's create this and now we need to create a constructor for that we have a shortcut c t o r hit tab so the constructor has been created inside this we need to pass another object of i configuration and here we need to initialize our underscore configuration is equal to configuration that is step one now let's create api for registration so http post the http verb would be http post let's give a route registration and now public spring registration and here we need to use this registration class so let's copy this name and paste it here create object registration to use it we need to include our models namespace so hover on it and use login this namespace so see the namespace has been added and now we can use it perfect so let's return blank screen so now we need some more name spaces so first of all we will use sql connection so for that we will use system dot data dot sql client so you see in the last install package system dot data so click on first and install so it will install the package which is sql dot data dot sql client this one so sql connection now create object con equal new sql connection and here i connection string configuration dot get connection string and here we need to keep the connection string name dot tostring go to your app settings.json copy this name and paste it here so our connection is ready now sql command cmd is equal to new sequel command comma connection object and here we will use inline query so insert into registration this is our table name if you see here this is the table name so sql is not case sensitive so you can use lower or capital letters and now we need to give our failed names so first name is username another one is password and last one is email and another one is is active values registration dot username let's copy this comma comma comma so first is you username second one is for password third one is for email and the last one is for this active save after this entire is equal to cmd dot execute non-query if i is greater than 0 then return data inserted or else simply return error so this i will be greater than zero if this record will get inserted otherwise it will return less than zero so let's save this now try to run this project so we'll try to execute this method first so let me open postman so if you see on the screen this localhost colon44315 this is our base url so we'll use this url and then we'll append our action method names so the api is up and running let's copy this url so click on plus it will open a new request change it to post paste your url go to ps visual studio copy this controller name registration without any space and after that the method name we also need to use api and go to body raw select json and we need to create a object so the first object should be username admin and password one two three test dot test gmail.com and last properties is active copy one try to send it the connection state okay so now let's stop this put it a debugger point here so before executing this we need to open our connection so con dot con dot open and after the executing query connection dot connection dot close save run the project again go to postman click on send go to visual studio so let's try to debug one by one see if i hover on i so it says one click on continue so you see we got the response as data inserted let's go to sql server and execute this command select from registration you see we have our record perfect let's also create one more record let me remove this breakpoint from the visual studio let me create a username by my name password should be same and email is manoj dashwall.html.com okay click on send data inserted select stop from registration okay so our registration is working perfect go to visual studio stop the solution now let's create a login api so login api again this would be a post api because we will pass our use email and password from the front end for front end you can use react angular jquery html whatever j you want you can use even you can use m web api mvc dot not mvc so login public string login and we will use the same class registration because this class has all the properties like username and password fsu here username and password so sql data adapter dj equal new data adapter comma let's open this and copy sql connection comma connection object and here we will use timeline query select star from registration where email is equal to registration dot email and password is equal to registration dot password and is active should be one so exactly one means that user is active so after that data table for that we need a namespace system.data dt equal new data table semicolon and d a dot fill dt if dt dot rows dot count is greater than zero means if we get any record from the database by using this command then return data found else written invalid user well let's say here valid user save put a breakpoint here start the project so let's copy this entire url click on a new tab paste it here and post let's copy this body body raw json paste here we only need two properties email and password so remove others click on send sorry my mistake we need to change the route now if i select again registration so now we have three records so let's change the url so click on send go to visual studio perfect you see this is a valid user perfect let me remove that breakpoint if i change the password let's say i entered the invalid password then you see invalid user all right so this is how you can simply create your login and registration api both should be the post because we will pass some data for this station maybe you will pass 10 properties five properties or maybe 20 properties based on your project and for login i'm using email and password but in most of the cases we use username and password and in some applications we use phone number and based on that phone number we generate a sms right so but the simplest way is this which i have just mentioned here this registration and this login so if you are looking for any paid project service like if you are working for a company you are doing internship and you are facing any problem difficulties in your project like in react.js.net sql server or if you are working on your final year project so i do these kind of paid services you can contact me on my instagram my instagram handle is open programmer and you can also find my other contact details on my channel description so this is about today's video i hope you like it if you like it hit the like button and share comment and if you haven't subscribe my channel please do subscribe i need your help and support so i'll see you in the next one thanks for watching take care bye
Info
Channel: Manoj Kumar
Views: 61,706
Rating: undefined out of 5
Keywords: login and registration with asp.net core, login and registration with asp.net core web api, asp.net core web api, asp.net core web api login, asp.net core web api registration, login with asp.net core web api, login with .net core web api, login with asp .net core api, asp.net core api login example, login in asp.net core web api, web api login using asp.net core, asp.net core web api login sample, how to login using asp.net core web api, how to use asp.net core web api, js
Id: 1qgY8U5ceOs
Channel Id: undefined
Length: 22min 52sec (1372 seconds)
Published: Wed Aug 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.