我建的用户表里有个字段“state”表示用户是否在线,
state=0——离线,
state=1——已登陆。
用户登陆的时候将state=1,这样如果异地登陆时判断到state=1,则弹出,该用户已登陆。
如何做到用户退出呢?也就是如何将用户在退出、关闭浏览器、或者 关机的时候 将state从新改为0状态
BS项目,这问题一直没解决,求高手解答,如果可能的话请给出代码,万分感谢
------解决方案--------------------------------------------------------
登录的时候,将用户加入Session
在Global.cs中通过方法Session_End()来判断用户是否已经离开
- C# code
protected void Session_End(Object sender, EventArgs e) { //进入该方法,才已经离开,也就是当前会话已经结束,回写用户登录状态state=0——离线 }
------解决方案--------------------------------------------------------
当你新建网站的时候(文件->新建网站->Asp.Net 网站),项目会自动生成一个Global.asax的文件,在其下面的Global.asax.cs中,你会看到
- C# code
void Session_End(object sender, EventArgs e) { // 在会话结束时运行的代码。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 InProc 时,才会引发 Session_End 事件。 // 如果会话模式设置为 StateServer // 或 SQLServer,则不会引发该事件。 }
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
Global.asax哪个是全局应用程序
------解决方案--------------------------------------------------------
楼主的需求应该是 单点登录(sso):
- C# code
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; }}