有没有办法确定ASP.NET应用程序中具有活动会话的用户数?我在特定应用程序中有一个管理员/工具页面,我想显示有关所有打开会话的信息,例如会话数,可能还有请求计算机的地址,或每个用户的其他凭据信息.
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方法用于防止多个线程同时更改此变量.
验证SessionState是否为"InProc"以使其正常工作
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性能计数器可以帮助您.然后,您只需从管理页面中阅读并显示性能计数器即可.