.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

'Remember Me' option in ASp.net

Posted By:Athira Sreejith       Posted Date: December 31, 2008    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

the following coding implements the functionality of Remember Me option when user logs in.
It stores credentials in cookie and restore when next time user logs in.



protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["myCookie"] != null)
{
HttpCookie cookie = Request.Cookies.Get("myCookie");
txtUserName.Text = cookie.Values["username"];
txtPassword.Text = cookie.Values["password"];
}
}
}

protected void btnLogin_Click(object sender, EventArgs e)
{
try
{
objImpl = new WebImplIPhone();
bool IsAvailable = false;
HttpCookie myCookie = new HttpCookie("myCookie");
bool IsRemember = chkRememberMe.Checked;

IsAvailable = objImpl.CheckUserLogin(txtUserName.Text, txtPassword.Text);
if (IsAvailable)
{
DataTable dtUserName = 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("ApplicationList.aspx");
}
else
{
lblError.Text = "Invalid UserName or Password or else your Username blocked";
}
}
catch (Exception ex)
{
Response.Redirect("Home.aspx");
}
}

     

Further Readings:

Responses
Author: PankajGupta         Company URL: http://www.dotnetspark.com
Posted Date: July 11, 2009

Thanks for the information. I am new to the Asp.Net but when i will learn something then i will use this code to implement on my project website.

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend