Use this..
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.Drawing;
public partial class AdminLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
clearCookies();
txtUname.Focus();
}
private void clearCookies()
{
if (Request.Cookies.Count > 0)
{
Request.Cookies.Clear();
Response.Cookies["UserData"].Expires = DateTime.Now.AddDays(-1);
}
}
protected void BtnLogin_Click(object sender, EventArgs e)
{
try
{
bool lStatus = verifyLogin();
if (lStatus)
{
DataTable dts = (DataTable)ViewState["UserData"];
HttpCookie userData = new HttpCookie("UserData");
DateTime dt = DateTime.Now;
userData["Username"] = txtUname.Text.ToLower();
userData["Token"] = dts.Rows[0]["Token"].ToString();
userData.Expires = dt.AddMinutes(20);
Response.Cookies.Add(userData);
if (dts.Rows[0]["Token"].ToString() == "111")
{
Response.Redirect("Home.aspx");
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Invalid Credentials", "alert('User Does Not Exists..!')", true);
lblerr.Text = "Incorrect user(or) Password";
lblerr.ForeColor= Color.Red;
}
}
catch (Exception ee)
{
}
}
public bool verifyLogin()
{
bool sts = false;
string Query = "select * from tablename_login where Username='" + txtUname.Text.ToLower() + "' and password='" + txtPwd.Text + "' ";
DataTable dt = DAL.getData(Query);
if (dt.Rows.Count > 0)
{
sts = true;
ViewState["UserData"] = dt;
}
return sts;
}
protected void btncancel_Click(object sender, EventArgs e)
{
Response.Redirect("AdminLogin.aspx");
}
}