登录页面新建一个方法名,传入用户ID,并引用此方法即可
//单点登录 private void isLogined(string id) { Hashtable hOnline = (Hashtable)Application["Online"]; if (hOnline != null) { int i = 0; while (i < hOnline.Count) { IDictionaryEnumerator idE = hOnline.GetEnumerator(); string strKey = ""; while (idE.MoveNext()) { if (idE.Value != null && idE.Value.ToString().Equals(id)) { //already login strKey = idE.Key.ToString(); hOnline[strKey] = "XXXXXX"; break; } } i = i + 1; } } else { hOnline = new Hashtable(); } hOnline[Session.SessionID] = id; Application.Lock(); Application["Online"] = hOnline; Application.UnLock(); }
新建一个CommonPage页面,系统中所有的页面都继承于CommonPage页
public partial class CommonPage : System.Web.UI.Page { public CommonPage() { // // TODO: 在此处添加构造函数逻辑 // } override protected void OnInit(EventArgs e) { Hashtable hOnline = (Hashtable)Application["Online"]; if (hOnline != null) { IDictionaryEnumerator idE = hOnline.GetEnumerator(); while (idE.MoveNext()) { if (idE.Key != null && idE.Key.ToString().Equals(Session.SessionID)) { if (idE.Value != null && "XXXXXX".Equals(idE.Value.ToString())) { hOnline.Remove(Session.SessionID); Application.Lock(); Application["Online"] = hOnline; Application.UnLock(); string js = "<script language=javascript>alert('{0}');parent.location.href='{1}';</script>"; Response.Write(string.Format(js, "帐号已在别处登录 ,你将被强迫下线(若非本人登录,请注意保护密码安全)!", "login.aspx")); //Response.Write("<script>document.getElementById('iframe').src='login.aspx';</script>"); return; } break; } } } } protected void Page_Load(object sender, EventArgs e) { } }
注意:关于继承,如下
public partial class allNews : CommonPage
新建一个Global.asax,Session_End中添加如下代码
protected void Session_End(object sender, EventArgs e) { Hashtable hOnline = (Hashtable)Application["Online"]; if (hOnline[Session.SessionID] != null) { hOnline.Remove(Session.SessionID); Application.Lock(); Application["Online"] = hOnline; Application.UnLock(); } }