Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the code for my Login page. The user enters their username and password and then presses the Login button.

C#
protected void LoginBtn_Click(object sender, EventArgs e)
        {
            if (userName.Text != "" && Password.Text != "")
            {
                //creates a connection to the database
                dbConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" +
                    Request.PhysicalApplicationPath + "App_Data\\Mercure.accdb");
                dbConnection.Open();

                string queryString = "SELECT * FROM LoginDetails WHERE Username = '" + userName.Text +
                    "' AND Password = '" + Password.Text + "'";

                try
                {
                    OleDbCommand cmd = dbConnection.CreateCommand();
                    cmd.CommandText = queryString;
                    OleDbDataReader dbReader = cmd.ExecuteReader();

                    if (dbReader.HasRows)
                    {
                        //setup cookies
                        HttpCookie loggedInCookie = new HttpCookie("LoggedIn", "true");
                        Response.Cookies.Add(loggedInCookie);

                        //username and password was correct and username being stored
                        Session["Username"] = userName.Text;
                        
                        if (userName.Text.StartsWith("EM"))
                        {
                            //move to employee page
                            Response.Redirect("employeeHome.aspx");
                        }
                        else
                        {
                            //move to client reservation page
                            Response.Redirect("Reservations.aspx");
                        }

                    }
                    else
                    {
                        LoginError.Text = "An incorrect username or password was entered!";
                    }
                }

                catch (Exception ex)
                {
                    LoginError.Text = "The following error occurred: " + ex.Message.ToString();
                }
                finally
                {
                    dbConnection.Close();
                }
            }
            else
            {
                LoginError.Text = "Please enter your details in the textboxes.";
            }
        }


This is the code on my master page for my login and logout links. My logout code also does not work... it still allows the user to go to pages that should only be accessible if they are logged in:
C#
namespace Mercure_Online_Booking
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //checks if cookies exists, and if the login value is true
            //if it does exist and is true, it enables the logout button
            if (Request.Cookies["LoggedIn"] != null
                && Request.Cookies["LoggedIn"].Value == "true")
            {
                logoutLinkBtn.Visible = true;
                LoginLinkBtn.Visible = false;
            }
            else if (Request.Cookies["LoggedIn"] != null
                && Request.Cookies["LoggedIn"].Value == "false")
            {
                logoutLinkBtn.Visible = false;
                LoginLinkBtn.Visible = true;
            }
        }

        protected void LoginLinkBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }

        protected void logoutLinkBtn_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["LoggedIn"] != null)
            {
                HttpCookie myCookie = new HttpCookie("LoggedIn");
                myCookie.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(myCookie);
                myCookie = null;
                HttpContext.Current.Session.Clear();
                Session.RemoveAll();
                HttpContext.Current.Session.Abandon();
            }
            
            Response.Redirect("Home.aspx");
        }

    }
}
Posted
Comments
Neema Derakhshan 15-Oct-14 3:25am    
is it an asp.net website ?
Member 9451624 15-Oct-14 3:30am    
Yes it is sorry, I forgot that part.

Hi, I think the problem is here in the if condition, can you double check. I feel it both the conditions will not satisfy if the cookie vale is null.

C#
protected void Page_Load(object sender, EventArgs e)
        {
            //checks if cookies exists, and if the login value is true
            //if it does exist and is true, it enables the logout button
            if (Request.Cookies["LoggedIn"] != null
                && Request.Cookies["LoggedIn"].Value == "true")
            {
                logoutLinkBtn.Visible = true;
                LoginLinkBtn.Visible = false;
            }
            else if (Request.Cookies["LoggedIn"] != null
                && Request.Cookies["LoggedIn"].Value == "false")
            {
                logoutLinkBtn.Visible = false;
                LoginLinkBtn.Visible = true;
            }
        }
 
Share this answer
 
Thank you, I checked it and i changed my code in the following way:

C#
protected void Page_Load(object sender, EventArgs e)
        {
            //checks if cookies exists, and if the login value is true
            //if it does exist and is true, it enables the logout button
            if (Request.Cookies["LoggedIn"] != null
                && Request.Cookies["LoggedIn"].Value == "true")
            {
                logoutLinkBtn.Visible = true;
                
            }
            else if(Request.Cookies["LoggedIn"] == null || //if cookie does not exist
              (Request.Cookies["LoggedIn"] != null && //if cookie is valid
              Request.Cookies["LoggedIn"].Value != "true")) //but user is not logged in
            {
                LoginLinkBtn.Visible = true;
                logoutLinkBtn.Visible = false;
            }
        }


It is working now.
 
Share this answer
 
I think the part that's making it not to work is the condition where you checked if the cookie object is not null, you should change it to if it's null(for enabling the logout button if not signed in)

C#
protected void Page_Load(object sender, EventArgs e)
        {
            //checks if cookies exists, and if the login value is true
            //if it does exist and is true, it enables the logout button
            if (Request.Cookies["LoggedIn"] != null
                && Request.Cookies["LoggedIn"].Value == "true")
            {
                logoutLinkBtn.Visible = true;
                LoginLinkBtn.Visible = false;
            }
            else if (Request.Cookies["LoggedIn"] == null
                && Request.Cookies["LoggedIn"].Value == "false")
            {
                logoutLinkBtn.Visible = false;
                LoginLinkBtn.Visible = true;
            }
        }
 
Share this answer
 
Instead of cookies please check your session["Username"] is available or not.

If your session value is null then show logout button or hide.

if(session["Username"] != null)
{
logoutLinkBtn.Visible = true;
LoginLinkBtn.Visible = false;
}
else
{
logoutLinkBtn.Visible = true;
LoginLinkBtn.Visible = false;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900