我正在做关于开发网站的项目。我是 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");
}