功能描述:A用户使用AID登录 ,B用户在A用户未退出的情况下也使用AID登录 这时A用户被取消权限
在Global.asax中添加以下代码:
void
Application_AcquireRequestState(object sender, EventArgs e)
{
if (Request["MyCookies"] != null && Session["ID"] != null)
{
HttpCookie cookie = Request.Cookies["MyCookies"];
if (cookie[Session["ID"].ToString()] != null)
{
if (Application[Session["ID"].ToString()].ToString() != cookie[Session["ID"].ToString()].ToString())
{
TimeSpan ts = new TimeSpan(-1, 0, 0, 0);
cookie.Expires = DateTime.Now.Add(ts);
Response.AppendCookie(cookie);
Session["ID"] = null;
Request.Cookies.Remove("MyCookies");
}
}
}
}
在需要权限验证的页面(等入窗口)
protected
void Button1_Click(object sender, EventArgs e)
{
if
(Request.Cookies["MyCookies"] != null)
{
if (Session["ID"] != null)
{
if (Session["ID"].ToString() != this.TextBox1.Text)
{
Res_Cookie();
}
else
{
Response.Write("登录成功!");
}
}
}
else
{
Res_Cookie();
}
}
private
void Res_Cookie()
{
string this_time = DateTime.Now.ToString();
Application[this.TextBox1.Text] = this_time;
HttpCookie cookie = new HttpCookie("MyCookies");
TimeSpan ts = new TimeSpan(0, 0, 1, 0);
cookie.Values.Add(this.TextBox1.Text, this_time);
Session["ID"] = this.TextBox1.Text;
Response.AppendCookie(cookie);
Response.Write("登录成功!");
}
在添加个测试按钮:
protected
void Button2_Click(object sender, EventArgs e)
{
if
(Request.Cookies["MyCookies"] == null)
{
Response.Write("登录失败!");
}
else
{
Response.Write("登录成功!");
}
}
最后在session_end事件中对Application中的数据进行释放