365教育平台官网

用户在线、离线列表 (续)

用户在线、离线列表 (续)

前面讲到用Cache来实现在线用户存储,今天主要改为单列模式来实现这个列表存储。

OnlineUser.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Web;

namespace ClassLibrary1

{

public class OnlineUser

{

private Dictionary dd = new Dictionary();

private static OnlineUser s_Instance = new OnlineUser();

private object m_OnlineMembers_Locker = new object();

///

/// 在线列表池的唯一实例

///

public static OnlineUser Instance

{

get { return s_Instance; }

}

public void CreateOnLine()

{

lock (m_OnlineMembers_Locker)

{

if (dd.ContainsKey(HttpContext.Current.Session.SessionID))

{

dd.Remove(HttpContext.Current.Session.SessionID);

}

dd.Add(HttpContext.Current.Session.SessionID, HttpContext.Current.User.Identity.Name);

}

}

public Dictionary GetDD()

{

return dd;

}

public void Remove(string userId)

{

dd.Remove(userId);

}

}

}

Login

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using ClassLibrary1;

namespace WebApplication8

{

public partial class WebForm1 : System.Web.UI.Page

{

private static object o = new object();

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

if (Request.Cookies["Name"] != null && Request.Cookies["Password"] != null)

{

this.txtName.Text = Request.Cookies["Name"].Value;

this.txtPwd.Attributes["value"] = Request.Cookies["Password"].Value;

}

}

}

protected void btnLogin_Click(object sender, EventArgs e)

{

if (MemberInfo.GetUserList().Contains(this.txtName.Text))

{

Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1);

Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);

if (CheckBoxRememberMe.Checked)

{

Response.Cookies["Name"].Expires = DateTime.Now.AddDays(7);

Response.Cookies["Password"].Expires = DateTime.Now.AddDays(7);

}

Response.Cookies["Name"].Value = this.txtName.Text.Trim();

Response.Cookies["Password"].Value = this.txtPwd.Text.Trim();

var ticket = new System.Web.Security.FormsAuthenticationTicket(1, this.txtName.Text, DateTime.Now, DateTime.Now.AddDays(1), false, "USER");

var encryptedTicket = System.Web.Security.FormsAuthentication.Encrypt(ticket);

if (Request.Cookies[System.Web.Security.FormsAuthentication.FormsCookieName] != null)

Request.Cookies.Remove(System.Web.Security.FormsAuthentication.FormsCookieName);

var loginIdentify = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName);

loginIdentify.Expires = DateTime.Now.AddDays(1);

loginIdentify.Value = encryptedTicket;

Response.AppendCookie(loginIdentify);

Session.Abandon();

if (!String.IsNullOrEmpty(Request.QueryString["ReturnUrl"]) && !Request.QueryString["ReturnUrl"].ToLower().Contains("profile"))

Response.Redirect(Server.UrlDecode(Request.QueryString["ReturnUrl"]));

else

Response.Redirect(System.Web.Security.FormsAuthentication.DefaultUrl);

}

}

public void CreateCacheOnLine()

{

Dictionary DD = new Dictionary();

if (HttpContext.Current.Cache["cacheName"] != null)

{

DD = (Dictionary)HttpContext.Current.Cache["cacheName"];

if (DD.ContainsKey(HttpContext.Current.Session.SessionID))

{

DD.Remove(HttpContext.Current.Session.SessionID);

}

}

DD.Add(HttpContext.Current.Session.SessionID,this.txtName.Text);

HttpContext.Current.Cache.Insert("cacheName", DD, null, DateTime.Now.AddMinutes(60), System.Web.Caching.Cache.NoSlidingExpiration);

}

}

}

WebForm.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication8.WebForm2" %>

WebForm2.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using ClassLibrary1;

using System.Web.Security;

namespace WebApplication8

{

public partial class WebForm2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

// ClassLibrary1.OnlineUser.Instance.CreateOnLine();

}

protected void Button1_Click(object sender, EventArgs e)

{

FormsAuthentication.SignOut();

Session.Abandon();

Response.Redirect(Request.Url.ToString());

}

}

}

Handler1.ashx

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using ClassLibrary1;

namespace WebApplication8

{

///

/// Handler1 的摘要说明

///

public class Handler1 : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

context.Response.ContentType = "text/plain";

System.Text.StringBuilder sb = new System.Text.StringBuilder();

Dictionary dd = OnlineUser.Instance.GetDD();

sb.Append("{\"userList\":[");

foreach(var userList in MemberInfo.GetUserList())

{

if (dd.ContainsValue(userList))

sb.Append("{\"memberName\":\""+userList+"\",\"onLine\":\"yes\"},");

else

sb.Append("{\"memberName\":\"" + userList + "\",\"onLine\":\"no\"},");

}

sb.Remove(sb.Length-1, 1);

sb.Append("]}");

context.Response.Write(sb.ToString());

}

public bool IsReusable

{

get

{

return false;

}

}

}

}

Global.asax

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.SessionState;

using ClassLibrary1;

namespace WebApplication8

{

public class Global : System.Web.HttpApplication

{

protected void Application_Start(object sender, EventArgs e)

{

}

protected void Session_Start(object sender, EventArgs e)

{

Application.Lock();

if (HttpContext.Current.User.Identity.IsAuthenticated)

{

OnlineUser.Instance.CreateOnLine();

}

Application.UnLock();

}

protected void Application_BeginRequest(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();

OnlineUser.Instance.Remove(Session.SessionID);

Application.UnLock();

}

protected void Application_End(object sender, EventArgs e)

{

}

}

}

相关推荐

造一艘海船要多少钱(造一艘海船要多少钱人民币)
28365365体育

造一艘海船要多少钱(造一艘海船要多少钱人民币)

📅 2025-07-10 👁️ 5103
成都的租车软件哪个好:全面比较与用户反馈
365教育平台官网

成都的租车软件哪个好:全面比较与用户反馈

📅 2025-06-29 👁️ 296
白色系分类色卡RGB与16进制HEX色值与名称对照表大全
365教育平台官网

白色系分类色卡RGB与16进制HEX色值与名称对照表大全

📅 2025-07-04 👁️ 1081
足球报分析日本队表现出色的原因:森保一留任、留洋、和强队约战