Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="1Login.aspx.cs" Inherits="Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 107px;
}
.auto-style3 {
width: 107px;
height: 29px;
}
.auto-style4 {
height: 29px;
}
.link
{
text-align:center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style4" colspan="2"><CENTER>LOGIN</td>
<td class="auto-style4"><CENTER></td>
</tr>
<tr>
<td class="auto-style3">USERNAME</td>
<td class="auto-style4">
<asp:TextBox ID="UserTextBox" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserTextBox" Display="Dynamic" ErrorMessage="Please Enter UserName" ForeColor="#FF3300" SetFocusOnError="True"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">PASSWORD</td>
<td>
<asp:TextBox ID="PassTextBox" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="UserTextBox" Display="Dynamic" ErrorMessage="Please Enter Password" ForeColor="#FF3300" SetFocusOnError="True"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Button ID="LoginButton" runat="server" Text="LOGIN" OnClick="LoginButton_Click" />
</td>
</tr>
<tr>
<td class="link" colspan="2">
<a href="Register.aspx">Not Registered Yet ? Click Here</a>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LoginButton_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
string query = "select * from signup where username=@user and password=@pass";
SqlCommand cmd = new SqlCommand(query,con);
cmd.Parameters.AddWithValue("@user", UserTextBox.Text);
cmd.Parameters.AddWithValue("@pass",PassTextBox.Text);
con.Open();
SqlDataReader dr =cmd.ExecuteReader();
if(dr.HasRows)
{
Session["user"] = UserTextBox.Text;
Response.Redirect("Login1.aspx");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Login Failed !!')</script>");
}
con.Close();
}
}
Logout.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="1Login1.aspx.cs" Inherits="Login1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Logout" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Logout.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Login1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Session["user"] != null)
{
Response.Write("Welcom to My Site" + Session["user"].ToString());
}
else
{
Response.Redirect("Login.aspx");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if(Session["user"]!=null)
{
Session["user"] = null;
Response.Redirect("Login.aspx");
}
}
}
0 Comments