How to implement remember me functionality using cookie in asp.net ?


Cookies
Hi

So many time we will get scenario to remember some frequently used data on browser only for example userId and password of some site. Then we can do this task using cookies in asp.net like this


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["UserName"] != null)
        {
            txtuserName.Text = Request.Cookies["Username"].Value;
        }

    }
    protected void btnClick_Click(object sender, EventArgs e)
    {

        if (CheckBox1.Checked)
        {
            Response.Cookies["Username"].Value = txtuserName.Text;
            Response.Cookies["Username"].Expires = DateTime.Now.AddDays(7);
        }
    }
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.