我建的用户表里有个字段“state”表示用户是否在线,
state=0——离线,state=1——已登陆。用户登陆的时候将state=1,这样如果异地登陆时判断到state=1,则弹出,该用户已登陆。如何做到用户退出呢?也就是如何将用户在退出、关闭浏览器、或者 关机的时候 将state从新改为0状态BS项目,这问题一直没解决,求高手解答,如果可能的话请给出代码,万分感谢

解决方案 »

  1.   

    登录的时候,将用户加入Session
    在Global.cs中通过方法Session_End()来判断用户是否已经离开
    protected void Session_End(Object sender, EventArgs e)   {    //进入该方法,才已经离开,也就是当前会话已经结束,回写用户登录状态state=0——离线    }
      

  2.   

    可能是我建的项目不对?我web项目里不存在app_code文件夹啊?,这一点我真的不太懂,请懂的给说明白,非常感谢
      

  3.   

    当你新建网站的时候(文件->新建网站->Asp.Net 网站),项目会自动生成一个Global.asax的文件,在其下面的Global.asax.cs中,你会看到
     void Session_End(object sender, EventArgs e) 
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 InProc 时,才会引发 Session_End 事件。
            // 如果会话模式设置为 StateServer 
            // 或 SQLServer,则不会引发该事件。    }
      

  4.   

    Global.asax哪个是全局应用程序
      

  5.   

    Global.asax哪个是全局应用程序
      

  6.   

    楼主的需求应该是 单点登录(sso):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;
    using System.Web.Caching;
    using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            int i = this.checkLogin(txtName.Text, txtPwd.Text);
            if (i > 0)
            {
                // 作为唯一标识的str_Key,应该是唯一的,这可根据需要自己设定规则。
                // 做为测试,这里用用户名和密码的组合来做标识;也不进行其它的错误检查。 
                // 生成str_Key
                string str_Key = txtName.Text + "_" + txtPwd.Text;
                // 得到Cache中的给定str_Key的值
                string str_User = Convert.ToString(Cache[str_Key]);
                // Cache中没有该str_Key的项目,表名用户没有登录,或者已经登录超时
                if (str_User == String.Empty)
                {
                    // TimeSpan构造函数,用来重载版本的方法,进行判断是否登录。
                    TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
                    HttpContext.Current.Cache.Insert(str_Key, str_Key, null, DateTime.MaxValue, SessTimeOut, CacheItemPriority.NotRemovable, null);
                    Session["User"] = str_Key;
                    // 首次登录成功
                    Response.Write("<h2 style='color:red'>你好,登录成功!");
                }
                else
                {
                    // 在 Cache 中存在该用户的记录,表名已经登录过,禁止再次登录
                    Response.Write("<h2 style='color:red'>抱歉,您好像已经登录了!");
                    return;
                }        }
            else
            {
                Response.Write("用户名称或密码错误!!!");
            }
        }
        protected void btnCandel_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtPwd.Text = "";
        }
        public int checkLogin(string loginName, string loginPwd)
        {
            SqlConnection con = new SqlConnection("Server=(local);database=db_18;Uid=sa;Pwd=");
            SqlCommand myCommand = new SqlCommand("select count(*) from 系统管理员表 where 用户名称=@loginName and 密码=@loginPwd", con);
            myCommand.Parameters.Add(new SqlParameter("@loginName", SqlDbType.NVarChar, 20));
            myCommand.Parameters["@loginName"].Value = loginName;
            myCommand.Parameters.Add(new SqlParameter("@loginPwd", SqlDbType.NVarChar, 20));
            myCommand.Parameters["@loginPwd"].Value = loginPwd;
            myCommand.Connection.Open();
            int i = (int)myCommand.ExecuteScalar();
            myCommand.Connection.Close();
            return i;
        }
    }
      

  7.   

    一、简单说明:
    单点登录(Single Sign On)简称SSO,是目前比较流行的企业业务整合的解决方案之一。在开发企业门户网站或电子商务系统时,设计一个用户只能在同一个网站进行唯一登录的功能,可以避免一个用户名和密码在多个地址进行登录。
    二、技术要点:
    Cache对象主要用户Web应用程序的缓存,对于每个应用程序都需要创建Cache对象的一个实例,并且只要对应的应用程序域保持活动,该实例便保持有效,有段Cache对象实例的所有信息都需要通过HttpContext对象的Cache属性或Page对象的Cache属性来提供。
      

  8.   


    http://www.cnblogs.com/ymyglhb/archive/2009/04/22/1441414.html