In this article we
will see about how to create simple login and logout page in asp.net.
In websites when you
enter it will ask Username and password . when only you enter correct
usernameand password it will allow to enter into website.
First you have to
create a database in sqlserver. Start sqlserver management studio and click
connect button. Right clich databases in left panel and then select create new
database.give database name for example logindb. Expand logindb.in left panel
click . right click on tables. Select create table.
Give details of table
as shown in the following picture.
Now right click
on the row with the column name id.
Select set as primary key.if a column selected as primary key it should be not
null and unique. Click save button in the toolbar . give table name as login.
Right click on table
name login in left panel and select edit top 200 rows. Give the input as shown
in following picture.
Now open visual
stuidio and click on create new project click web tab bellow visual c#. right
dide panel click asp.net empty application. Givename for project. And click o.k right click project
name on the solution explorer and select
create new web form. It desiging code should abe as shown below.
<%@
Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="loginpage.login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css"
>
body
{
background-color : Gray ;
}
</style>
</head>
<body >
<form id="form1" runat="server">
<div>
<table style="margin:auto;border:5px solid
white;">
<tr><td>
<asp:Label ID="Label1" runat="server"
Text="Enter user
Name:"></asp:Label></td>
<td>
<asp:TextBox ID="txtUserName"
runat="server"></asp:TextBox></td>
</tr>
<tr><td>
<asp:Label ID="Label2" runat="server"
Text="Enter
password"></asp:Label></td>
<td>
<asp:TextBox ID="txtpassword"
TextMode ="Password" runat="server"></asp:TextBox></td>
</tr>
<tr><td>
</td>
<td>
<asp:Button ID="btnLogin"
runat="server"
Text="Login"
onclick="btnLogin_Click"
/>
</td>
</tr>
<tr><td>
</td><td><asp:Label ID="lblErrorMessage"
runat="server"
ForeColor ="Red"
Text="Invalid
credentials"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Now the programming
script coding should be as below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace loginpage
{
public partial class login :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
lblErrorMessage.Visible = false;
}
protected void
btnLogin_Click(object sender, EventArgs e)
{
using (SqlConnection
sqlcon = new SqlConnection(@"Data Source=MUTHU1-PC\SQLEXPRESS;Initial
Catalog=logindb;Integrated Security=True;"))
{
sqlcon.Open();
string strqry = "select
count(1) from login where username=@username and password=@password";
SqlCommand sqlcmd = new
SqlCommand(strqry, sqlcon);
sqlcmd.Parameters.AddWithValue("@username",
txtUserName.Text.Trim());
sqlcmd.Parameters.AddWithValue("@password",
txtpassword.Text.Trim());
int count =Convert.ToInt32
(sqlcmd.ExecuteScalar());
if (count == 1)
{
Session["user"]
= txtUserName.Text.Trim();
Response.Redirect("welcome.aspx");
}
else
{
lblErrorMessage.Visible = true;
}
}
}
}
}
In the above coding I
have given the data soucrde name of sqlconnection as MUTHU1-PC\SQLEXPRESS in
your coding it should be your server name,(if you don’t know the server name
open sql server management studio click. And watch the server name in connect
dialog box)
According to
programming code above if we give correct username and password it will
redirect to page welcome.aspx. otherwise it will show error message. At the
same time if we give the correct username and password our username will be
saved in a session variable called user.
Design coding for
welcome.aspx
<%@
Page Language="C#" AutoEventWireup="true" CodeBehind="welcome.aspx.cs" Inherits="loginpage.welcome" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css"
>
body
{
background-color :Gray
;
}
</style>
</head>
<body bgcolor="#0066ff">
<form id="form1" runat="server">
<div>
<table>
<tr><td></td><td>
<asp:Label ID="Lblmessage"
runat="server"
Text=""></asp:Label></td>
</tr>
<tr><td></td>
<td>
<asp:Button ID="Btnlogout"
runat="server"
Text="logout"
onclick="Btnlogout_Click"
/></td>
</tr></table>
</div>
</form>
</body>
</html>
Welcome.aspx.cs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace loginpage
{
public partial class welcome :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
if(Session["user"]==null)
Response .Redirect ("login.aspx");
Lblmessage.Text = "username:"
+ Session["user"];
}
protected void
Btnlogout_Click(object sender, EventArgs e)
{
Session.Abandon();
Response.Redirect("login.aspx");
}
}
}
According to programming
code above when we get in to welcome.aspx page it will show our user name. if
we directly enter into welcome.aspx page it will redirect to login page and ask
username and password. If we click logout button we are logged out using the
code Session.Abandon();and also redirect
ourselves in to login page.
This is the simple way of creating login and logout in
asp.net .
--thank
you.
I am taking full dotnet
course in madurai,Tamilnad,india. Interested people may contact me in the
following number 9196293 29142
FULL DOTNET SYLLABUS
C#,VISUAL C#,VISUAL
BASIC.NET,ASP.NET,ADO.NET, WPF, WCF, SQLSERVER, LINQ,MVC,RAZOR,AJAX,JAVASCRIPT,
JQUERY,ANGULAR JS,GRIDVIEW.
Muthu karthikeyan,Madurai.