Page

Login Page and Logout Page Code using Cookies and Sessions

// LOGIN Code //


using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session.RemoveAll();  //it Removes all Sessions that previously stored
}
// the Below code is for the Login Code in Buttion1_Click
protected void Button1_Click(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
string sql = null;
connetionString = “Your Connection String”;
sql = “select * from login where username='” + TextBox1.Text + “‘ AND Password='” + TextBox2.Text + “‘”;
connection = new SqlConnection(connetionString);
connection.Open();
command = new SqlCommand(sql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds,”tempLogin”);
adapter.Dispose();
command.Dispose();
connection.Close();

//This Code is used for the Useraname and Password Checking
if (ds.Tables[0].Rows.Count>0
{
Session[“x”] = TextBox1.Text;
Session.Timeout = 1;
HttpCookie c = new HttpCookie(“userdetails”);
c[“user name”] = TextBox1.Text;
//c[“Password”] = TextBox2.Text;
c.Expires = DateTime.Now.AddMinutes(1);
Response.Cookies.Add(c);
Response.Redirect(“Home.aspx”);
}
}
}


// LOGOUT Code //


using System.Collections;
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// This Code is used to Disable the Browser “BACK Button”
Response.ClearHeaders();
Response.AppendHeader(“Cache-Control”, “no-cache”); //HTTP 1.1
Response.AppendHeader(“Cache-Control”, “private”); // HTTP 1.1
Response.AppendHeader(“Cache-Control”, “no-store”); // HTTP 1.1
Response.AppendHeader(“Cache-Control”, “must-revalidate”); // HTTP 1.1
Response.AppendHeader(“Cache-Control”, “max-stale=0″); // HTTP 1.1
Response.AppendHeader(“Cache-Control”, “post-check=0″); // HTTP 1.1
Response.AppendHeader(“Cache-Control”, “pre-check=0″); // HTTP 1.1
Response.AppendHeader(“Pragma”, “no-cache”); // HTTP 1.1
Response.AppendHeader(“Keep-Alive”, “timeout=3, max=993″); // HTTP 1.1
Response.AppendHeader(“Expires”, “Mon, 26 Jul 1997 05:00:00 GMT”); // HTTP 1.1
//This code is used to maintain UserName in the Home page using Session and Cookies
        if (Session[“x”] == null)
{
Response.Redirect(“Default.aspx”);
}
else
{
IEnumerator mc;
mc = Request.Cookies.AllKeys.GetEnumerator();
while (mc.MoveNext())
{
if (Request.Cookies[mc.Current.ToString()].HasKeys == true)
{
IEnumerator sc;
sc = Request.Cookies[mc.Current.ToString()].Value.GetEnumerator();
while (sc.MoveNext())
{
Response.Write(sc.Current.ToString() + Request.Cookies[mc.Current.ToString()][sc.Current.ToString()]);
}
}
}
}
}
//This Code is for the LOGOUT Code
protected void Button1_Click(object sender, EventArgs e)
{
Session.Clear();
Session.RemoveAll();
Session.Abandon();
Response.Redirect(“Default.aspx”, true);
}
}

No comments:

Post a Comment