Breaking News

Editors Picks

Friday, February 25, 2011

'Remember Me' option in ASp.net


'Remember Me' option in ASp.net



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;
using System.IO;

namespace WebApplication1
{
    public partial class WebForm4 : System.Web.UI.Page
    {


        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 mymethod();
                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("dashboard.aspx");
                }
                else
                {
                    lblError.Text = "Invalid UserName or Password or else your Username blocked";
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("Default.aspx");
            }
        }
     
    
    }
}

No comments :

Post a Comment

Contact Us

Name

Email *

Message *