How to Write a Simple login page in Asp.net

fc/DotNetFundaArticles?bg=EB7F30&fg=000000&anim=0" /%

Yesterday I stumbled across a post that was posted on our forum. I was very busy with other things, when Sheo started a conversation based on that post. I was looking at it until he updated the untagged code that was posted. By looking at the Stored Procedure I could see that the poster was lost and his Stored Procedure approach failed him. In this Article am going to explain how to write a simple login in Asp.net.

Using the code

We are going to user C# as our language.

Open Visual Studio and Create a New Website. Automatically you will have an empty page defined for you like this

Go to Design View and you will notice there is nothing on your page. Now open your Toolbox and add a buttons and some textbox and depicted in the following.

And your Design should look like this

Now as you can see our login page is created, Let us see how we can validate the login and what is needed to have a proper login. Open your Sql management Studio and Create a New Database, but if you already have it you will just follow the Step 2 where we add a table.

Step 1: Create a Database

Step 2: Create a Table

Step 3: Let us Add Sample Data

Now that we have our sample Data. Please note that you can use any other field but the username and Password fields are the most important. Now let us create our stored Procedure.

Step 4: Create a Stored Prcedure that will validate and return a valid Integer.

In the above Stored Procedure we count the Records that have matched the Records and if there is one record found then it is a good login else it is a bad login. But

how will you use this in your asp.net Page. First we have to create a Function that will access the stored procedure and call that function in click event of the button. Create a Function as show below in your page not inside your page load because you will get an Error.

As you can see this Function return an Integer, as we said before this will return either a 1 which is equal to “Good” and other numbers will be “Bad”. The login Data should be clean, no Duplicates should be there because this will break your functionality. It might return the duplicates and the count might not match the if statement that you will see later in this article. Double click you Button and add the following code in the Click event of the Button.

Now our code is ready for testing. Run your Application and enter an incorrect password deliberately and see what message you see and when you enter the correct login you will receive a message that says

Login is Good, Send the User to another page or enable controls

In your application you can redirect the user to another page and store the Session that you will use through out your application And abondon when the user exit your application. Please note that for some application it is good to enable and disable Controls based on the Session value, meaning that you can check if the user is logged in. and display the benefits that logged in user can get in the same page. I will not explain more on that because it is beyong the scope of this article.

Conclusion

There are a lot of ways to do a login control in asp.net, but I thought it will be important to point the basic one to our users.

Thank you for visiting DotnetFunda

Source: www.dotnetfunda.com

Category: Forex

Similar articles: