【发布时间】:2014-12-09 19:04:05
【问题描述】:
我有 Asp.net 网站项目。我正在使用 Global.asax 文件查找活动用户号(活动会话);
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["UsersOnline"] = 0;
}
void Session_Start(object sender, EventArgs e)
{
}
void Session_End(object sender, EventArgs e)
{
Application.Lock();
Application["UsersOnline"] = (int)Application["UsersOnline"] -1 ;
Application.UnLock();
}
这是登录页面,我在用户登录时增加应用程序状态;
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string connectionstring = WebConfigurationManager.ConnectionStrings["LocalSqlServerTracking"].ConnectionString;
string sqlstring;
sqlstring = "Select Name,Password,ID,SessionStateID from Employee where Name=@UserName and Password=@Password";
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand command = new SqlCommand(sqlstring, con);
command.Parameters.Add(new SqlParameter("@UserName", Login1.UserName));
command.Parameters.Add(new SqlParameter("@Password", Login1.Password));
System.Data.SqlClient.SqlDataReader reader;
// open a connection with sqldatabase
try
{
using (con)
{
con.Open();
reader = command.ExecuteReader();
Boolean read= reader.Read();
online = Convert.ToInt32(reader["SessionStateID"]);
if (read)
{
Session["Name"] = Login1.UserName;
Session["Password"] = Login1.Password;
Session["ID"] = reader["ID"].ToString();
int ID = Convert.ToInt32(Session["ID"]);
var matches = from p in entities.Employees
where p.ID ==ID
select p;
// Execute the query and return the entity object.
Employee emp = matches.Single();
// Change the entity object.
emp.SessionStateID = 0;
// Commit the changes back to the database.
entities.SaveChanges();
Application["UsersOnline"] = (int)Application["UsersOnline"] + 1;
e.Authenticated = true;
}
else {
Label1.Text = "User not exist";
e.Authenticated = false;
}
现在如果我登录 Applications State 增加到 1,注销并登录后仍然为 1,如果使用新标签 以相同用户登录 或 另一个用户应用程序状态增加到2。效果很好。但是经过一段时间后,我登录并看到负值,例如 -7 。我唯一能想到的是会话超时。如果我使用 Session.Abandon() 一段时间后会话超时仍然有效吗?我将如何防止它,不应该是这样,或者如果有什么我看不到它会是什么?
<forms loginUrl="login.aspx" defaultUrl="userHome.aspx" timeout="60" />
<sessionState cookieless="true" cookieName="ASP.NET_SessionId" regenerateExpiredSessionId="false" timeout="5" mode="InProc"></sessionState>
【问题讨论】:
-
您是否在您的网站中使用了最喜欢的图标?是 MS MVC 吗?我在论坛上发现了一些用户抱怨 Session_Start 被调用两次的帖子。有人说这是因为他们使用了
favicon.ico(很奇怪)。无论如何,您仍然应该尝试映射 looged 用户以检查是否有效。 -
没有常规的 Asp 网站项目。
标签: c# asp.net session global-asax