日期:2014-05-17 浏览次数:20648 次
protected void Session_End(Object sender, EventArgs e)
{
//进入该方法,才已经离开,也就是当前会话已经结束,回写用户登录状态state=0——离线
}
------解决方案--------------------
当你新建网站的时候(文件->新建网站->Asp.Net 网站),项目会自动生成一个Global.asax的文件,在其下面的Global.asax.cs中,你会看到
void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 InProc 时,才会引发 Session_End 事件。
// 如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
}
------解决方案--------------------
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 SqlParamet