ASP.NET Tutorial 6- Create a Login website - Login page & Validating User and Password in database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome once again in the last few videos I have been showing you how to make a registration page in asp.net and how to link it with the database now in this video I will show you how to make a login page which verifies the username and password from the database and then redirect you to the secure access page which you want to show after the login so let's get started by creating the login page first of all so I will go to my project right click it and I will say the add new item and will show me this dialog box I will choose webform and I will say this is my login form so login dot aspx and I will say add and once again this is a blank form so I will say this will be my login page and I can change the font also first of all I make it bold and I will say they should be extra-large 36 et okay and I can also change the justification or justify Center so this will always be in the center now we are having our heading now we will add a table to our page so I will say in third table this time also we will take three columns and let's take four rows and click OK and first of all I will say user name second row will be password and okay for now we are okay with this so I will select this and this both the rows and I will say justify right now I will go to my standard tool box and I will take a text box here and text box for the password okay now I have both the text boxes I select both text boxes and I will say width is equal to 1/8 t PX property is wrong because this should be 0 okay now this is done now I will take a validation tool this all this thing I have also shown you in the registration form so I will just make it a little bit faster I will take a required field value data 1 & 2 and I will change the text so I will go select my required field validator and I will change the error message as please and the user name and second is second error message is please enter password okay now I have posted things I will change first of all the ID of this text box so I know which text box I am talking about first text box name is use a name text box user name and sexes second text box ID is text box password okay now I'm done with that I will link my a quite field validator with this textbox so I will select my required field value data and I will say control to validate and I will still choose textbox username from here and second required field validator or password I will choose control to validate will be my textbox password and here I will select my password field and I will say the text mode of this is password so your character doesn't show once this is done I will take a button from my standard tool box and I will say this will be my login button and I will change also the ID button underscore login okay so now we are ready with our login page you can just also change the font size of these two also so I will select my required field validator I will go to the for for color and I will say red okay now this is done once your login page is designed just like your login button and double click it and first of all I will add here using system dot data dot SQL client dot SQL client okay and second is I will add second namespace as system dot configuration okay now once you are done with that let's try and write the code for this button login click event so for that what I will do is whatever code I have written for the registration page for the database connection I will copy and paste it here also because it will be the same more or less same not exactly same so I will just copy whatever I have written in page load event okay I'm not copying button click event code which I have written for database but I am copying this page load event and accept or inside this if is post back condition so I will copy this I will go through the code also so don't worry if you haven't seen it and I will go to my Loeb login dot aspx dot CS form and I will paste my code here let me make it a little bit wider and bigger so you will be able to see it so first of all I will just declare my SQL connection Korn is equal to new SQL connection and I will say as an argument I will pass configuration manager dot connection strings and in the square bracket I am passing my registration connection string which this is the connection string from the my database if you have some confusion you you can see my previous videos and you will be able to know what this registration connection string is and after the square bracket just right dot connection string and close the bracket and now we will open our connection with the database so right conn dot open and in the next line we prepare our query so i will write String check user is equal to and this is the query which we have let me open this server Explorer so we will see the database also at the same time so I will right here select count and in the bracket I will say stars from my table name so this is my table name from the database where username is equal to now it will take some time to open so what I wanted to show you in the solution Explorer that we have made this table called user data in the earlier videos and it has five column ID username email password and country right so what a query I am writing is select count in the bracket star from my table name which is this where username which is this column name is equal to whatever I enter in the username text box so now my user name textbox is little bit different in the login form so I will select my username text box and I will copy my textbox ID and I will paste it here so we remove the error so write where username is equal to single quote in between double quote plus plus and then write this text box store text okay and in the next video I will prepare a command so I will write SQL command dot which is comm the object name is equal to new SQL command and I will pass my query as the first argument and second argument is the connection with the database now I will right int temp is equal to convert dot two in 32 and the argument right calm which is this calm dot execute scaler dot two string and I will close my connection okay and earlier what we are doing is if the user already exists then we say user already exists okay now this time what we will do is inside this stamp is equal to 1 we will write the code once again for verifying the user name and password so let's try and code the user login code so what I will write here is first of all I will open the connection once again so I will write con not open and in the second line I will make my query so I will write string I will say check password query for example long name is equal to and double quote and semicolon and in here I will write the query I will say select password from my table name which is user data where my username is equal to whatever I enter in the text box I will copy just the same code and I will paste here instead of this okay so my query looks like this and in the next line what I will do is I will just copy this SQL command and I will paste it here and instead of this query I will just replace this query by this and I will change the constant come come on name so I will say pass calm now okay this is done now what I will do is I will write in the next line string because we are verifying the string and then we will say pass word is equal to and then I will say we copy my password command dot execute scalar and make it dot to string okay once this is done then it will pass your password in this string from this query so whatever user name you write here in the text box it will search in the database and it will find the password and then it will give you in this string because we are saying this selects password from the user table right okay now in the next line we will verify the password from the password which you user has entered in the password text box so what I will do is I will give a if condition if and let me close the brackets I will say if my password which of this password is equal to the password which user has entered so I will just select my password text field and I will copy my ID for the text P text field for the password and then I will say text box password dot text so if this password matches the password which user has entered that means our password is correct right so here we will say response dot right and we will say password first of all double quote and then I will say pass word is correct okay so once this is done we know that our password is correct right and if this is not the case then we will write else I will give the response password is not correct right so I will just copy response but right and I will say password is not correct and after this if condition where we have written F temp is equal to 1 we will say here also else and we will make a bracket and here also we will give the response that if username is not present so you say username is not current so all right username is not correct okay so once this all is done let me make a new session also after the login is correct your login is correct yes right fashion and the square bracket right new is equal to and what ever is your text box for the user name ID is so text box user name dot text okay so now this is done I think this will work this code is now done so let's try and run the program so I will run my program from here or I will go to my login page and I will right-click it and I will say view in the browser so right click and view in browser and I will say yes I want to save the data and it opens my login page hopefully no error occurs okay so there is no error let me see what that I have in the database so I will go to my solution Explorer I will right-click my table and say show table data it will show me the data which is there in the table so I have user name admin and with respect to the admin I have the password one two three four five so let me enter this username admin so I will write admin and the password is one two three four five and I will say login and it's saying password is not correct why it's saying this I have observed sometimes when you enter your username and password it this string password has some space is unwanted spaces which you don't want so just right here string password is equal to password dot execute scalar dot to string and just right after this to string dot right to replace and what we are going to replace is we are going to replace all the single spaces with no space right so just right double quote and give us space and in the second argument just write no space okay now once again right-click and view in the browser yes and this time I will say my username in is admin and password is one two three four five why it's not in turn I will say one two three four five and say login and now you can see my password is correct okay so sometimes this phenomena comes if this phenomena comes you need to replace all the spaces with no spaces okay let's try some other username and passwords for example mark which have the same password let's try it for this Julie and four times three okay let's try username and the password three three three three and I will say login and he says password is correct let me enter the wrong password so I will enter some random password and I will login and this time it says password is not correct let me not enter anything in the password field and it says please enter the password when ice and nothing in the username it says please enter the username what next you can do is one more thing you can do if your password is correct you can redirect your user to the new page so for example I will write this pawn store redirect and for example this is an example I will create or you can create a new page which is exclusively for the login user so I will say double quote and select any page for example manager so I will write manager dot aspx and let me try and run the program once again view in browser and username is admin password is one two three four five and when I login it will lead me or redirect me to the new page so let's try and you can see there is manager page so in this page it you can design your own data which is which the only login user can see okay so now I have shown you how to login create a login page and how to login with the verification of username and password I hope you enjoyed this video please rate comment and subscribe and bye for now
Info
Channel: ProgrammingKnowledge
Views: 582,947
Rating: undefined out of 5
Keywords: ASP.NET (Software Framework), C# (Programming Language), Website (Ontology Class), Google, .NET Framework (Programming Language), Tutorial, Microsoft Visual Studio (Software), Intro, Google Search (Website), First, Introduction, Database (File Format Genre), Password
Id: QoPABrUknsE
Channel Id: undefined
Length: 22min 58sec (1378 seconds)
Published: Mon Dec 16 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.