Pages

Sunday, 15 April 2012

How to Store Username and Passwords in cookies


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

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            HttpCookie obj1, obj2;
            obj1=Request.Cookies["uname"];
            obj2 = Request.Cookies["pwd"];
            if (obj1 != null && obj2 != null)
            {
                Response.Write(obj1.Value);
                if (obj1.Value == "username" && obj2.Value == "password")
                {
                    Server.Transfer("welcome.aspx");
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (t1.Text == "username" && t2.Text == "password")
        {
            if (c1.Checked)
            {
                HttpCookie e1, e2;
                e1 = new HttpCookie("uname");
                e2 = new HttpCookie("pwd");
                e1.Value = t1.Text;
                e2.Value = t2.Text;
               
                e1.Expires = DateTime.Now.AddMinutes(2);
                e2.Expires = DateTime.Now.AddMinutes(2);
                Response.AppendCookie(e1);
                Response.AppendCookie(e2);
            }
            Server.Transfer("welcome.aspx");
        }// login parameter verification
        else
        {
            Response.Write("Invalied Login");
        }
    }
}

No comments:

Post a Comment