Credit portal




How to Make a Login Form Using Session in ASP.Net C#

how to expire session in asp net

This article shows how to make a Login Form using session state in ASP.Net C#. It has the two inputs username and word and a login button. When the user clicks the login button the user is redirected to a new page (his account page). In this account page we have a logout button. When he clicks he goes out from his account page and returns to his login page with the saved username and word, the user doesn't need to fill it in again, it is saved in the session using a browser cookie.


Open your Visual Studio 2010 and create an Empty Website, provide a suitable name (LoginForm_demo).

In Solution Explorer you get your empty website, then add two web forms and a SQL Server Database as in the following.

For Web Form:

LoginForm_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it Login_demo.aspx. Again get to the same process and add another web form and name it Redirectpage.aspx.

For SQL Server Database

LoginForm_demo (your empty website) then right-click then select Add New Item -> SQL Server Database. (Add a database inside the App_Data_folder).


In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table. Make the table like this.

Table tbl_data (Don't Forget to make ID as IS Identity -- True)

Figure 1: Data Table

Now enter some data in your database by going to Tables then right-click then select Show Table Data and enter whatever you want to add in the username and word. I had entered this data.

Figure 2: Enter the Data

We will match this username and word so if the data is correct, the user is redirected to his account page or otherwise the user gets an error message.

Now make some design for your application by going to Login_demo.aspx and try the code as in the following.

You will get something like this:

Category: Forex

Similar articles: