在登录按钮中输入如下代码
private void Button1_Click(object sender, System.EventArgs e)
{
if(TextBox1.Text=="abc" & TextBox2.Text=="abc")//和数据库验证通过,假设用户名密码均为"abc"
{
//继续判断是否该用户已经登陆
Hashtable h=(Hashtable)Application["Online"];
if(h!=null)
{
//判断哈希表中是否有该用户
IDictionaryEnumerator e1=h.GetEnumerator();
bool flag=false;
while(e1.MoveNext())
{
if(e1.Value.ToString()=="abc")
{
flag=true;
break;
}
}
if(flag) //已经登陆
{
Label1.Text="Login already!";
return;
}
}
else
{
h=new Hashtable();
}
//保存用户到Application中
h[Session.SessionID]="abc";
Application["Online"]=h;
}
else
{
TextBox1.Text="";
TextBox2.Text="";
Label1.Text="User/Password failed!";
return;
}
}
Global.asax文件中改写方法
protected void Session_End(Object sender, EventArgs e)
{
Hashtable h=(Hashtable)Application["Online"];
if(h[Session.SessionID]!=null)
h.Remove(Session.SessionID);
Application["Online"]=h;
}
本文介绍了一种基于ASP.NET的用户登录验证方法,并详细解释了如何使用哈希表跟踪在线用户的状态,包括用户登录检查流程及Session结束时的处理。
675





