我用如下代码实现单点登录,可是碰到一个问题,就是进入登录页面后,我要退出来,该如何清除Cache!用SESSION.CLEAR不行!在退出按钮代码该如何实现啊????
protected void btnLogin_Click(object sender, EventArgs e)
{
int i = this.checkLogin(txtName.Text,txtPwd.Text);
if (i> 0)
{
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 btnLogin_Click(object sender, EventArgs e)
{
int i = this.checkLogin(txtName.Text,txtPwd.Text);
if (i> 0)
{
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("用户名称或密码错误!!!");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货