'Remember Me' option in ASp.net
using System;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
using System.IO;
namespaceWebApplication1
{
public partial class WebForm4 : System.Web.UI.Page
{
protectedvoid Page_Load(objectsender, EventArgs e)
{
if(!IsPostBack)
{
if(Request.Cookies["myCookie"] != null)
{
HttpCookiecookie = Request.Cookies.Get("myCookie");
txtUserName.Text = cookie.Values["username"];
txtPassword.Text = cookie.Values["password"];
}
}
}
protectedvoid btnLogin_Click(objectsender, EventArgs e)
{
try
{
objImpl = new mymethod();
boolIsAvailable = false;
HttpCookiemyCookie = new HttpCookie("myCookie");
boolIsRemember = chkRememberMe.Checked;
IsAvailable = objImpl.CheckUserLogin(txtUserName.Text, txtPassword.Text);
if(IsAvailable)
{
DataTabledtUserName = objImpl.ReadUserIdbyUserName(txtUserName.Text);
if(dtUserName != null)
{
if (dtUserName.Rows.Count == 1)
{
Session["UserId"] = dtUserName.Rows[0].ItemArray[0].ToString();
}
}
if(IsRemember)
{
myCookie.Values.Add("username", txtUserName.Text);
myCookie.Values.Add("password", txtPassword.Text);
myCookie.Expires = DateTime.Now.AddDays(15);
}
else
{
myCookie.Values.Add("username", string.Empty);
myCookie.Values.Add("password", string.Empty);
myCookie.Expires = DateTime.Now.AddMinutes(5);
}
Response.Cookies.Add(myCookie);
//FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, chkRememberMe.Checked);
Response.Redirect("dashboard.aspx");
}
else
{
lblError.Text = "Invalid UserName or Password or else your Username blocked";
}
}
catch(Exception ex)
{
Response.Redirect("Default.aspx");
}
}
}
}
0 comments:
Post a Comment