有没有办法确定 ASP.NET 应用程序中拥有事件 session 的用户数量?我在特定应用程序中有一个管理/工具页面,我想显示有关所有打开 session 的信息,例如 session 数量,可能还有请求计算机的地址或每个用户的其他凭据信息。
最佳答案
在global.aspx中
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["OnlineUsers"] = 0;
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate
// mode is set to InProc in the Web.config file.
// If session mode is set to StateServer or SQLServer,
// the event is not raised.
Application.Lock();
Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
Application.UnLock();
}
注意: Application.Lock 和 Application.Unlock 方法用于防止多个线程同时更改此变量。
在 Web.config 中
验证 SessionState 是否为“InProc”才能正常工作
<system.web>
<sessionState mode="InProc" cookieless="false" timeout="20" />
</system.web>
在您的 .aspx 文件中
Visitors online: <%= Application["OnlineUsers"].ToString() %>
注意:代码最初复制自 http://www.aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx (链接不再有效)
关于asp.net - 如何确定 ASP.NET 站点 (IIS) 上的用户数量?他们的信息呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/155593/