利用Cache、Timer(ATLAS)控制用户重复登陆的可行性方法
点击次数:27 次 发布日期:2008-11-26 11:43:30 作者:源代码网
|
源代码网推荐 源代码网推荐 我们需要怎么来改进这个时间的难题呢?设置一个较短的失效时间间隔,然后每隔一定时间,检查一下Cache,把用户登陆信息重新写入Cache。那么只要用户不退出网站系统,或者不关闭浏览器,这种判断方法将会一直有效!那么,在WEB上,在ASP.NET下,什么东西能方便的实现计时器的效果呢?目前而言,最好的选择无疑是 ATLAS 中的Timer控件!能够设置计时器的启动,间隔时间,以及间隔时间后做的事件。 源代码网推荐 源代码网推荐 程序改进以后,分享如下,请参看程序注释: 源代码网推荐 前台页面 源代码网推荐 源代码网推荐 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 源代码网推荐 源代码网推荐 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 源代码网推荐 <html xmlns="http://www.w3.org/1999/xhtml"> 源代码网推荐 <head runat="server"> 源代码网推荐 <title>Untitled Page</title> 源代码网推荐 </head> 源代码网推荐 <body> 源代码网推荐 <form id="form1" runat="server"> 源代码网推荐 <asp:ScriptManager ID="ScriptManager1" runat="server" /> 源代码网推荐 <div> 源代码网推荐 <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 源代码网推荐 <ContentTemplate> 源代码网推荐 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 源代码网推荐 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="登陆" /> 源代码网推荐 <br /> 源代码网推荐 <br /> 源代码网推荐 <asp:Label ID="Label1" runat="server" Width="350px"></asp:Label> 源代码网推荐 <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="清除Cache" /> 源代码网推荐 <asp:Timer ID="Timer1" runat="server" Enabled="False" Interval="15000" OnTick="Timer1_Tick"> 源代码网推荐 </asp:Timer> 源代码网推荐 </ContentTemplate> 源代码网推荐 </asp:UpdatePanel> 源代码网推荐 </div> 源代码网推荐 </form> 源代码网推荐 </body> 源代码网推荐 </html> 源代码网推荐 源代码网推荐 后台程序 源代码网推荐 源代码网推荐 using System; 源代码网推荐 using System.Data; 源代码网推荐 using System.Configuration; 源代码网推荐 using System.Web; 源代码网推荐 using System.Web.Security; 源代码网推荐 using System.Web.UI; 源代码网推荐 using System.Web.UI.WebControls; 源代码网推荐 using System.Web.UI.WebControls.WebParts; 源代码网推荐 using System.Web.UI.HtmlControls; 源代码网推荐 源代码网推荐 public partial class _Default : System.Web.UI.Page 源代码网推荐 { 源代码网推荐 protected void Page_Load(object sender, EventArgs e) 源代码网推荐 { 源代码网推荐 源代码网推荐 } 源代码网推荐 protected void Button1_Click(object sender, EventArgs e) 源代码网推荐 { 源代码网推荐 try 源代码网推荐 { 源代码网推荐 //用户名 源代码网推荐 string sName = TextBox1.Text; 源代码网推荐 源代码网推荐 //生成Key 源代码网推荐 string sKey = sName + "_Login"; 源代码网推荐 源代码网推荐 //得到Cache中的给定Key的值 源代码网推荐 string sUser = Convert.ToString(Cache[sKey]); 源代码网推荐 源代码网推荐 //检查是否存在 源代码网推荐 if (sUser == null || sUser == String.Empty) 源代码网推荐 { 源代码网推荐 Session["username"] = sName; 源代码网推荐 源代码网推荐 //Cache中没有该Key的项目,表明用户没有登录,或者已经登录超时 源代码网推荐 //TimeSpan 表示一个时间间隔,获取系统对session超时作的设置值 源代码网推荐 //(如果考虑到允许用户再次登陆的时间小于session超时时间,可将此值设小) 源代码网推荐 //TimeSpan SessTimeOut = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0); 源代码网推荐 //这里为了演示,把Cache保存时间间隔设置为了20秒 源代码网推荐 TimeSpan SessTimeOut = new TimeSpan(0, 0, 0, 20, 0); 源代码网推荐 HttpContext.Current.Cache.Insert( 源代码网推荐 sKey, 源代码网推荐 sKey, 源代码网推荐 null, 源代码网推荐 DateTime.MaxValue, 源代码网推荐 SessTimeOut, 源代码网推荐 System.Web.Caching.CacheItemPriority.NotRemovable, 源代码网推荐 null 源代码网推荐 ); 源代码网推荐 源代码网推荐 //启动Timer 源代码网推荐 this.Timer1.Enabled = true; 源代码网推荐 源代码网推荐 //首次登录,您可以做您想做的工作了。 源代码网推荐 Label1.Text = "你好!" + sName + "欢迎光临"; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 //在Cache中发现该用户的记录,表示已经登录过,禁止再次登录 源代码网推荐 Label1.Text = "对不起,你的用户身份已登陆"; 源代码网推荐 return; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 catch (System.Exception ex) 源代码网推荐 { 源代码网推荐 Label1.Text = ex.Message; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 protected void Button2_Click(object sender, EventArgs e) 源代码网推荐 { 源代码网推荐 //用户名 源代码网推荐 string sName = TextBox1.Text; 源代码网推荐 源代码网推荐 //生成Key 源代码网推荐 string sKey = sName + "_Login"; 源代码网推荐 源代码网推荐 //为了测试方便,设置了这个从Cache中移出登陆信息的方法 源代码网推荐 HttpContext.Current.Cache.Remove(sKey); 源代码网推荐 源代码网推荐 Label1.Text = Session["username"] + " 的用户登陆信息已从Cache清除!"; 源代码网推荐 } 源代码网推荐 protected void Timer1_Tick(object sender, EventArgs e) 源代码网推荐 { 源代码网推荐 if (Session["username"] != null) 源代码网推荐 { 源代码网推荐 //用户名 源代码网推荐 string sName = TextBox1.Text; 源代码网推荐 源代码网推荐 //生成Key 源代码网推荐 string sKey = sName + "_Login"; 源代码网推荐 源代码网推荐 //得到Cache中的给定Key的值 源代码网推荐 string sUser = Convert.ToString(Cache[sKey]); 源代码网推荐 源代码网推荐 TimeSpan SessTimeOut = new TimeSpan(0, 0, 0, 20, 0); 源代码网推荐 if (sUser != null) 源代码网推荐 { 源代码网推荐 HttpContext.Current.Cache.Remove(sKey); 源代码网推荐 } 源代码网推荐 HttpContext.Current.Cache.Insert( 源代码网推荐 sKey, 源代码网推荐 sKey, 源代码网推荐 null, 源代码网推荐 DateTime.MaxValue, 源代码网推荐 SessTimeOut, 源代码网推荐 System.Web.Caching.CacheItemPriority.NotRemovable, 源代码网推荐 null 源代码网推荐 ); 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 this.Timer1.Enabled = false; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 示例代码:/Files/heekui/WebLogin.rar 源代码网推荐 源代码网推荐 后记: 源代码网推荐 1 这个方法对于判断用户重复登陆是可行的,但是同时伴随着另一个问题点。设置了Timer,定时工作的话,只要不是正常退出,或者关闭浏览器的话,Session便永远不会失效了。这样作会有什么不好的效果吗? 源代码网推荐 2 这个方法对每一个用户而言都会定时向服务器发出请求,无疑会增加服务器端的负担。若同时在线人数很多的情况下,这种请求是否会对服务器产生很大的影响。 源代码网推荐 所以,只能说以上的这个方法只是一种可行的方法,但是否最优,没有测试。不知各位还有什么更好的办法没有。 源代码网推荐 http://www.cnblogs.com/heekui/archive/2007/01/08/615254.html 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
