0

我正在做关于开发网站的项目。我是 ASP.net 的新手,现在我有一个关于登录会话的问题,当用户登录时,同时在网上冲浪的每个人也会被记录为用户无论他们是否登录。只有当有人单击注销按钮并且每个人都注销时,会话才会结束。请帮我。每一个帮助将不胜感激。这是我的代码,我在我的母版页中编写了这些代码:

 protected void Page_Load(object sender, EventArgs e)

    {

        string equip = "Equipment.aspx";
        string url = HttpContext.Current.Request.Url.AbsoluteUri;
        if (url.IndexOf(equip) != -1) { Calendar2.Visible = false; Image1.Visible = false; }
        equip = "Bookings.aspx";
        if (url.IndexOf(equip) != -1) { Calendar2.Visible = false; Image1.Visible = false; }
        equip = "Rooms.aspx";
        if (url.IndexOf(equip) != -1) { Calendar2.Visible = false; Image1.Visible = false; }
        equip = "Users.aspx";
        if (url.IndexOf(equip) != -1) { Calendar2.Visible = false; Image1.Visible = false; }

        if (!Page.IsPostBack)
        {
            if (user != "" && user != null)
            {
                loginStatus = true;
                redirectpage = false;

            }
            else
            {

                redirectpage = false;
                loginStatus = false;
                user = "";
                authority = 0;
            }

        }
        else
        {

            if (user == "" || user==null)
            {

                if (cal != null)
                {

                }
                loginStatus = false;
                authority = 0;
            }
            else
            {
                if (cal != null)
                {

                }
                loginStatus = true;
            }
        }
    }

 /// <summary>
 /// Responds to a login request, validating details against the database and
 /// loading the user into the session if successful.
 /// </summary>
 /// <param name="sender">The sending object.</param>
 /// <param name="e">The event arguments.</param>

 protected void Login_Authenticate(object sender, AuthenticateEventArgs e)
 {

 SqlConnection oConn =
 new SqlConnection();
 oConn.ConnectionString = @"Data Source=STAVROS\SQLEXPRESS;User ID=sa;Password=123abc;Initial Catalog=webdev";

 sSQL = "select * from tbl_user where username = '" + Login.UserName + "'AND password = '" + Login.Password + "' ";
 SqlCommand oComm1 = new SqlCommand(sSQL, oConn);

 try
 {

 oConn.Open();
 SqlDataReader i = oComm1.ExecuteReader();
 if (i.HasRows)
 {
 while (i.Read())
 {
 user = i.GetString(0);
 authority = i.GetInt16(7);

 loginStatus = true;
 string url = HttpContext.Current.Request.Url.Absolute…
 string p = "Home.aspx";
 if (url.IndexOf(p) != -1) { Response.Redirect("Home.aspx"); }
 Helper.CreateUserSession(Session, user);
 }

 }
 else
 {
 loginStatus = false;
 Login.FailureText = "Invalid username or password.";
 }


 i.Close();
 }
 catch (Exception ex)
 {
 Response.Redirect("room-book.aspx");

 }

 }

 /// <summary>
 /// Logs the user out (kills the session)
 /// </summary>
 /// <param name="sender">The sender</param>
 /// <param name="e">The event arguments</param>

 protected void btnLogout_Click(object sender, EventArgs e)
 {

 loginStatus = false;
 user = "";
 authority = 0;
 Response.Redirect("Home.aspx");
 }
4

3 回答 3

1

显然,您对 Web 应用程序中的会话处理一无所知。

这是C# / ASP.NET 中基于表单的身份验证的综合教程。在继续之前,您需要阅读基本概念。

希望能帮助到你。

于 2012-07-28T04:02:14.423 回答
0

loginStatus 是我想的字段?如果是这样,您必须在每个会话的基础上存储此标志

于 2012-07-28T03:54:17.980 回答
-2

最好通过 Session 在应用程序中使用表单身份验证票证。连同该配置,Web Config 从 Windows 身份验证到 Passport 模式身份验证。MoreOver 使用 javascript 标签在 Log Out 的表单加载事件中使会话过期。

于 2012-07-28T04:41:53.803 回答