问题描述
我有与Global.asax一起添加的Asp.net网站项目。
I have Asp.net website project with Global.asax added i it.
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["UsersOnline"] = (int)Application["UsersOnline"] - 1;
}
和登录页面;
And Login Page;
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();
//Where i increase when user logged in
Application["UsersOnline"] = (int)Application["UsersOnline"] + 1;
e.Authenticated = true;
}
else {
Label1.Text = "User not exist";
e.Authenticated = false;
}
另外webconfig文件;
Also webconfig file;
<sessionState cookieless="true" cookieName="ASP.NET_SessionId" regenerateExpiredSessionId="false" timeout="5" mode="InProc"></sessionState>
<forms loginUrl="login.aspx" defaultUrl="userHome.aspx" timeout="5" />
我正在userHome中检索应用程序状态的值(经过身份验证后我移动到thi页面);
I am retrieving value of Application state in userHome(when authenticated i move to this page);
if (!this.IsPostBack)
{
Response.Write("Number of active sessions:"+Convert.ToString(Application["UsersOnline"]));}
如果我登录ApplicationState增加1。退出并登录将不会更改正常的应用程序状态。问题 在一定时间后应用程序状态值变为负数我无法理解。为什么我觉得Session.Timeout正在触发Session_End但是在注销后它应该是无效的。我做错了什么?我只是想看看当前的活跃用户。
If i log in ApplicationState increase by one.Logging out and logging in will not change the application state that is ok. Problem is after certain amount of time Application State value become negative which i cannot understand.Why i feel like Session.Timeout is firing the Session_End but after log out it should be ineffective.What i am doing wrong?I just want to see current active users.
推荐答案
这篇关于使用应用程序状态确定活动用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!