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

ASP.NET
Visual Studio.Net 内幕(7)
.Net 2.0 原汁原味读取注册表
ASP.NET 2.0中预设的cookie
ASP.NET:一段比较经典的多线程学习代码
ASP.NET2.0服务器控件之自定义状态管理
Asp.net给站点某目录增加Aspnet用户
ASP.NET技巧:请求网址并解析返回的html
在Asp.net用C#建立动态Excel
ASP.NET2.0:页面中链入的CSS、js文件带中文时需注意
ASP.NET技巧:数据岛出到Excel最为简易的方法
第一次用.net2.0 LOGIN登陆控件的困惑和解决方法
在ASP.net中保存/取出图片入/从SQL数据库
ASP.NET技巧:做个DataList可分页的数据源
在.NET中利用委托实现窗体间通信
在.NET2.0中使用自定义事务操作
解决 ASP.NET中目录访问权限的问题
ASP.Net防止刷新自动触发事件的解决方案
白刃之战:PHP vs. ASP.NET(节选)-架构比较
C#中使用SendMessage
提取HTML代码中文字的C#函数

ASP.NET 中的 signlog 登陆实现


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 169 ::
收藏到网摘: 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()

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