Tuesday, November 23, 2010

How to create a persistent cookie?

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class RememberMe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["UserName"] != null && Request.Cookies["PassWord"] != null)
        {
            txtUsername.Text = Request.Cookies["UserName"].Value;
            txtPassword.Text = Request.Cookies["PassWord"].Value;
       
       
        }
        else
        {
        HttpBrowserCapabilities objBrowsCap;
      
        objBrowsCap = Request.Browser;
        if (objBrowsCap.Cookies)
        {
            lblMessage.Text = "Cookies Enabled";
        }
        else
        {
            lblMessage.Text = "Cookies Disabled";
        }
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        HttpCookie UserNameCookie = new HttpCookie("UserName", txtUsername.Text.ToString());
        HttpCookie PasswordCookie = new HttpCookie("PassWord", txtPassword.Text.ToString());
        UserNameCookie.Expires = System.DateTime.Now.AddDays(1);
        PasswordCookie.Expires = System.DateTime.Now.AddDays(1);
        Response.Cookies.Add(UserNameCookie);
        Response.Cookies.Add(PasswordCookie);
        lblMessage.Text = "You have logged in successfully";

    }
}

No comments:

Post a Comment