当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > signlog 登陆实现

ASP.NET
用 Asp.Net 建立一个在线 RSS 新闻聚合器的方法
关于前台调用后台事件__doPostBack函数
Bin 和 App_Code 文件夹介绍
.NET 2.0 的压缩功能代码
解决Visual Studio 2005 无法显示设计视图的方法
asp.net(c#)两种随机数的算法,可用抽考题
asp.net下url传递中文的解决方案
XmlReader 读取器读取内存流 MemoryStream 的注意事项
asp.net下创建、查询、修改带名称空间的 XML 文件的例子
使用.NET存储XML数据的方法
XslTransform.Transform将结果输出到字符串里的方法
安装 VS2005 SP1 有关问题的解决办法
asp.net下中文验证码,免费开源代码
自定义应用程序配置文件(app.config)
asp.net下使用DIME协议上传文件
动态改变ASP.net页面标题和动态指定页面样式表的方法
WEB上调用HttpWebRequest奇怪问题的解决方法
HTTP协议下用Web Service上传大文件的解决方案
asp.net下Response.ContentType类型汇总
ASP.NET User Control使用技巧一则

ASP.NET 中的 signlog 登陆实现


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 162 ::
收藏到网摘: n/a

关于重新登陆!
1:在Global中的设置。
  //添加Init,并且添加AcquireRequestState事件;
  public void Init(HttpApplication application)
  {
   application.AcquireRequestState +=new EventHandler(application_AcquireRequestState);
  
  }
  //实现AcquireRequestState,在每次客户端response的时候都会执行这个事件;
  private void application_AcquireRequestState(object sender, EventArgs e)
  {
   System.Web.HttpApplication App = ((HttpApplication)sender);
   if(App.Context.Session == null) return;
   if(App.Context.Session["userID"] == null ) return;
   System.Data.DataTable dt = (System.Data.DataTable)Application["userTable"];
   if(dt.Select("userID = "+Session["userID"].ToString()).Length>0)
   {
    dt.Rows[0]["loginTime"] = System.DateTime.Now;
    dt.AcceptChanges();
   }
  

  }
 
  //Timer的间隔时间 
  private int interval = 20;
 
  //在Application_Start中  1:建立在线全局用户表; 2:注册timer事件(用于间隔一定时间来维护在线用户表)。
  protected void Application_Start(Object sender, EventArgs e)
  {
   //--1 application user table;
   System.Data.DataTable dt = new DataTable();
   dt.Columns.Add("userID");
   dt.Columns.Add("loginTime");
   dt.PrimaryKey = new System.Data.DataColumn[]{dt.Columns["userID"]};
   dt.AcceptChanges();
   Application.Lock();
              Application["userTable"] = dt;
   Application.UnLock();
   //--2 Timer
   System.Timers.Timer tm = new System.Timers.Timer();
   tm.Interval = 60000*this.interval;
   tm.Elapsed +=new System.Timers.ElapsedEventHandler(tm_Elapsed);
   tm.Start();
      
  }
 
  //timer事件; 
 private void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
   deleteTimeOutUser();
  }
  //删除过期的在线用户; 
 private void deleteTimeOutUser()
  {
   if(Application["userTable"] == null) return;
   System.Data.DataTable dt = (System.Data.DataTable)Application["userTable"];
   foreach(System.Data.DataRow dr in dt.Rows)
   {
    if(System.DateTime.Compare(((System.DateTime)dr["loginTime"]).AddMinutes(2),System.DateTime.Now)<0)
     dr.Delete();
   }
   dt.AcceptChanges();
  
  }
//------------------------------用户单击退出后--的过程-----------------------------------------
//退出       
public void reLogin(System.Web.UI.Page currentPage)
{
 if((currentPage.Session != null)&&(currentPage.Session["userID"] != null))
 {
  this.deleteUser(int.Parse(currentPage.Session["userID"].ToString()),currentPage.Application);
 }
 
 currentPage.Session.Abandon();
 
}

//删除当前用户在application中的userID;
private void deleteUser(int userID,System.Web.HttpApplicationState Application)
{
 if(Application["userTable"] == null) return;
 System.Data.DataTable dt = (System.Data.DataTable)Application["userTable"];
 foreach(System.Data.DataRow dr in dt.Rows)
 {
  if(int.Parse(dr["userID"].ToString()) == userID)
   dr.Delete();
 }
 dt.AcceptChanges();

}

在删除在线用户时如果程序使用了Form认证模式,还应该System.Web.Security.FormsAuthentication.Signout()

----------------------------------------------------------------------------
遗留问题,虽然解决了超时注销的问题,但用户直接