ASP.Net Session和Application在线用户应用

Global.asxa

using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;

namespace elcare
{
 /// <summary>
 /// Global 
 /// </summary>
 public class Global : System.Web.HttpApplication
 {
  /// <summary>

  /// </summary>

  private System.ComponentModel.IContainer components = null;

  public Global()
  {
   InitializeComponent();
  } 
  
  protected void Application_Start(Object sender, EventArgs e)
  {
   Application["OL_UserCount"]=0;
  }
 
  protected void Session_Start(Object sender, EventArgs e)
  { 
   Application.Lock();
   Application["OL_UserCount"]=(int)Application["OL_UserCount"]+1;//在线用户
   Application.Add(Session.SessionID.ToString(),1);//这里用SessionID作为Application名
   Application.UnLock();
  }

  protected void Application_BeginRequest(Object sender, EventArgs e)
  {
   
  }

  protected void Application_EndRequest(Object sender, EventArgs e)
  {

  }

  protected void Application_AuthenticateRequest(Object sender, EventArgs e)
  {
 
  }

  protected void Application_Error(Object sender, EventArgs e)
  {

  }

  protected void Session_End(Object sender, EventArgs e)
  {
   Application.Lock();
   Application.Remove(Session.SessionID.ToString());
   Application["OL_UserCount"]=(int)Application["OL_UserCount"]-1;
   Application.UnLock();
  }

  protected void Application_End(Object sender, EventArgs e)
  {

  }
   
  #region   /// <summary>
  /// </summary>
  private void InitializeComponent()
  {   
   this.components = new System.ComponentModel.Container();
  }
  #endregion
 }
}

登陆验证 aspx
  public void IdPassInSQL()
  {
   string strConn=(String) ((NameValueCollection) Context.GetConfig("system.web/database"))["strConn"];
   using (SqlConnection conn = new SqlConnection(strConn))
   {
    SqlCommand cmd = new SqlCommand("sp_IDPWD",conn);
    cmd.CommandType=CommandType.StoredProcedure;
    cmd.Parameters.Add("@ID",SqlDbType.VarChar,20);
    cmd.Parameters.Add("@PWD",SqlDbType.VarChar,20);
    cmd.Parameters["@ID"].Value=txtName.Text;
    cmd.Parameters["@PWD"].Value=txtPwd.Text;
    conn.Open();

    using (SqlDataReader dr = cmd.ExecuteReader())
    {
     if(dr.Read())
     {
      bool NotOL=true;
      for(int i=0;i<Application.Count;i++)
      {
       if(Application[Application.GetKey(i).ToString()].ToString()==dr["UserID"].ToString())
        NotOL=false;
      }
      if(NotOL)
      {
       Session["UserName"]=dr["UserName"].ToString(); //Session["UserName"] 用户名字
       Session["UserID"]=dr["UserID"].ToString(); //Session["UserID"] 用户ID
       Application[Session.SessionID.ToString()]=dr["UserID"].ToString();//给Session_Start生成的Application付用户ID
       Response.Redirect("./main/main.aspx");
      }
      else
       message.Text="该用户已登陆";
     }
     else
     {
      message.Text="密码错误";
     }
    }
   }
  }

注销退出
  private void btnQuit_Click(object sender, System.EventArgs e)
  {
   Session.Abandon();//该语句将调用Global的Session_End事件
   Response.Write("<script language=javascript>parent.location='../default.aspx'</script>");//
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现在线用户统计,你需要考虑以下步骤: 1. 创建一个名为 "OnlineUsers" 的 Session 变量,用于存储当前在线用户的 ID 列表; 2. 在 Global.asax 文件的 Session_Start 方法中,将当前用户的 ID 添加到 "OnlineUsers" 变量中; 3. 在 Global.asax 文件的 Session_End 方法中,将当前用户的 ID 从 "OnlineUsers" 变量中移除; 4. 在需要显示在线用户数量的视图中,从 "OnlineUsers" 变量中获取用户 ID 列表并计算数量; 5. 可以使用 SignalR 实现实时更新在线用户数量。 下面是一个简单的示例代码: 1. 创建一个名为 "OnlineUsers" 的 Session 变量并初始化为空列表: ```csharp Session["OnlineUsers"] = new List<string>(); ``` 2. 在 Global.asax 文件的 Session_Start 方法中,将当前用户的 ID 添加到 "OnlineUsers" 变量中: ```csharp protected void Session_Start(object sender, EventArgs e) { var onlineUsers = (List<string>)Session["OnlineUsers"]; onlineUsers.Add(User.Identity.Name); Session["OnlineUsers"] = onlineUsers; } ``` 3. 在 Global.asax 文件的 Session_End 方法中,将当前用户的 ID 从 "OnlineUsers" 变量中移除: ```csharp protected void Session_End(object sender, EventArgs e) { var onlineUsers = (List<string>)Session["OnlineUsers"]; onlineUsers.Remove(User.Identity.Name); Session["OnlineUsers"] = onlineUsers; } ``` 4. 在需要显示在线用户数量的视图中,从 "OnlineUsers" 变量中获取用户 ID 列表并计算数量: ```csharp var onlineUsers = (List<string>)Session["OnlineUsers"]; var onlineUserCount = onlineUsers.Distinct().Count(); ``` 5. 使用 SignalR 实现实时更新在线用户数量: ```csharp // 安装 Microsoft.AspNet.SignalR.Core 和 Microsoft.AspNet.SignalR.JS 包 // 创建一个名为 "OnlineUsersHub" 的 SignalR Hub 类 public class OnlineUsersHub : Hub { public void UpdateOnlineUserCount(int count) { // 调用所有连接的客户端的 "updateOnlineUserCount" 方法 Clients.All.updateOnlineUserCount(count); } } // 在 Global.asax 文件的 Application_Start 方法中启用 SignalR protected void Application_Start() { // ... RouteTable.Routes.MapHubs(); } // 在需要更新在线用户数量的地方,调用 SignalR Hub 的 "UpdateOnlineUserCount" 方法 var onlineUsers = (List<string>)Session["OnlineUsers"]; var onlineUserCount = onlineUsers.Distinct().Count(); var hubContext = GlobalHost.ConnectionManager.GetHubContext<OnlineUsersHub>(); hubContext.Clients.All.updateOnlineUserCount(onlineUserCount); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值