Showing posts with label logout. Show all posts
Showing posts with label logout. Show all posts

Thursday, 19 July 2012

asp.net login and logout code with clear page catch and history


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

namespace The_Kila.Admin
{
    public partial class Default1 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["kilaCon"].ToString());
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {//---check user login info
            try
            {
                if ((!string.IsNullOrEmpty(txtUserName.Text)) && (!string.IsNullOrEmpty(txtPassword.Text)))
                {


                    string cLogin = "select count(*) from login where username='" + txtUserName.Text.TrimStart().TrimEnd() + "' and password='" + txtPassword.Text.Trim() + "'";
                    SqlCommand cmd = new SqlCommand(cLogin, con); con.Open();
                    Int32 nUser = Convert.ToInt32(cmd.ExecuteScalar()); con.Close();

                    if (nUser > 0)
                    {
                        CreateCookiee();
                        Response.Redirect("~/Admin/adminHome.aspx");
                    }
                    else
                        Response.Write("<script>alert('Invalid UserName Or Password')</script>");
                }
                else
                    Response.Write("<script>alert('Please fill UserName and Password')</script>");
            }
            catch (Exception)
            {
                // Response.Redirect("~/Admin/Default.aspx");
                //throw;
            }
           
        }
        private void CreateCookiee()
        {//--login user create cookie
            HttpCookie _logCookie = new HttpCookie("userInfo");
            _logCookie["loginName"] = txtUserName.Text.TrimStart().TrimEnd();
           // _logCookie["password"] = txtPassword.Text.TrimStart().TrimEnd();
            _logCookie.Expires.AddMinutes(30);
            Response.Cookies.Add(_logCookie);
        }

    }
}
//--------------------
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;

namespace The_Kila.Admin
{
    public partial class AdminMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    HttpCookie _InfoCookie = new HttpCookie("userInfo");
                    if (_InfoCookie != null && (!string.IsNullOrEmpty(Convert.ToString(Request.Cookies["userInfo"]["loginName"]))))
                    {
                        string cLoginUser = Server.HtmlEncode(Request.Cookies["userInfo"]["loginName"]);
                        lblWelcomeLogin.Text = "Welcome -" + cLoginUser;
                    }
                    else
                        Response.Redirect("http://thekila.in/");
                }
            }
            catch (Exception)
            {
                Response.Redirect("~/Admin/Default.aspx");
               // throw;
            }
            
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {//--loout
                        
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ExpiresAbsolute = DateTime.Now.AddMonths(-1);
            Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(-1);
            HttpCookie aCookie = Request.Cookies["userInfo"];
            aCookie.Values.Remove("loginName");

            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();

            Response.Redirect("~/Admin/Default.aspx");
        }
    }
}