@Cerebrus
This is the Login page I created. but it does not involve any session
or cookies.
namespace Login
{
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(@"Data Source=SWATY
\SQLEXPRESS;Initial Catalog=Project_mydb;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
cmd.CommandText = "select * from Login";
cmd.Connection = conn;
da.SelectCommand = cmd;
da.Fill(ds, "Login");
int totaluser = ds.Tables["Login"].Rows.Count;
for (int i = 0; i < totaluser; i++)
{
if (txtUserName.Text == ds.Tables["Login"].Rows[i]
["Username"].ToString() && txtPassword.Text == ds.Tables["Login"].Rows
[i]["Password"].ToString())
{
Response.Redirect("Home.aspx");
}
else
{
Label3.Visible = true;
}
}
}
}
}
Besides that, I have tried creating cookies. Check out this code:
But I am not getting how to carry this cookie for an entire Login -
Logout session.
Also, I don't get how to destroy the cookie at logout.
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["Preferences"];
if (cookie == null)
{
lblWelcome.Text = "<b>Unknown Customer</b>";
}
else
{
lblWelcome.Text = "<b>Cookie Found.</b><br /><br />";
lblWelcome.Text += "Welcome, " + cookie["Name"];
}
}
protected void cmdStore_Click(object sender, EventArgs e)
{
// Check for a cookie, and only create a new one if
// one doesn't already exist.
HttpCookie cookie = Request.Cookies["Preferences"];
if (cookie == null)
{
cookie = new HttpCookie("Preferences");
}
cookie["Name"] = txtName.Text;
cookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(cookie);
lblWelcome.Text = "<b>Cookie Created.</b><br /><br />";
lblWelcome.Text += "New Customer: " + cookie["Name"];
Response.Redirect("Welcome.aspx");
}