如何根据用户是否登录显示“登录”和“注销”链接? [英] How do I display the Login and Logout links depending on if the user is logged in or not?

查看:73
本文介绍了如何根据用户是否登录显示“登录”和“注销”链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的登录页面的代码。用户输入他们的用户名和密码然后按下登录按钮。



Here is the code for my Login page. The user enters their username and password and then presses the Login button.

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:

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");
        }

    }
}

推荐答案

我认为问题是在if条件下,你可以仔细检查。如果cookie值为null,我觉得这两个条件都不会满足。



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.

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


谢谢,我查了一下,我用以下方式更改了我的代码:



Thank you, I checked it and i changed my code in the following way:

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.


I认为使其不起作用的部分是您检查cookie对象是否为空的条件,如果它为空则应将其更改为null(如果未登录则启用注销按钮)



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)

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


这篇关于如何根据用户是否登录显示“登录”和“注销”链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆