做了一个网页,用户要登录后才能访问出主页外的网页,登录注册框在主页一角。登录框下边有一个linkbutton,为退出登录。我的问题就是,如果用户已登录,必须点击退出登陆后才能进行下次登录,请问怎么用session控制,这是我的代码,大侠帮忙改下!谢谢!!我是这么想的:会员登录成功,置Session["user"] = "ok" ;管理员登录成功,
置Session["user"] = "abc",当再次登录时, 如发现Session["user"] = "ok"或者Session["user"] = "abc"即说明还没有退出登录。但如果将判断放在程序首段,就会报错,没有实例化,请问怎么解决??退出登录代码:if (Session["user"] == null)
{
this.Label4.Text = "您尚未登陆";
}
else
{
Session.Abandon();
Response.Redirect("退出登陆.aspx");
}
登录click事件:
protected void Button2_Click(object sender, EventArgs e)
{
//if (Session["user"].ToString() == "ok" || Session["user"].ToString() == "abc")
//{
// Page.ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script>alert('您已经登录,请先退出!');</script>");
//}
//else
//{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=localhost;Initial Catalog=电影信息交流系统;Integrated Security=True";
try
{
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
if ("会员" == DropDownList1.Text)
{
this.LinkButton1.Visible = true;
cmd.CommandText = "select * from 用户信息表 where 注册用户名 = '" + TextBox1.Text + "'and 密码='" + TextBox2.Text + "'";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["user"] = "ok";
Response.Redirect("欢迎会员.aspx");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script>alert('错误!请重新输入用户名或密码!');</script>");
}
dr.Close();
}
else if ("管理员" == DropDownList1.Text)
{
cmd.CommandText = "select * from 管理员表 where 管理员用户名 = '" + TextBox1.Text + "'and 密码='" + TextBox2.Text + "'";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["user"] = "abc";
MessageBox.Show("欢迎管理员");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script>alert('错误!请重新输入用户名或密码!');</script>");
}
dr.Close();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
cn.Close();
}
置Session["user"] = "abc",当再次登录时, 如发现Session["user"] = "ok"或者Session["user"] = "abc"即说明还没有退出登录。但如果将判断放在程序首段,就会报错,没有实例化,请问怎么解决??退出登录代码:if (Session["user"] == null)
{
this.Label4.Text = "您尚未登陆";
}
else
{
Session.Abandon();
Response.Redirect("退出登陆.aspx");
}
登录click事件:
protected void Button2_Click(object sender, EventArgs e)
{
//if (Session["user"].ToString() == "ok" || Session["user"].ToString() == "abc")
//{
// Page.ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script>alert('您已经登录,请先退出!');</script>");
//}
//else
//{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=localhost;Initial Catalog=电影信息交流系统;Integrated Security=True";
try
{
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
if ("会员" == DropDownList1.Text)
{
this.LinkButton1.Visible = true;
cmd.CommandText = "select * from 用户信息表 where 注册用户名 = '" + TextBox1.Text + "'and 密码='" + TextBox2.Text + "'";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["user"] = "ok";
Response.Redirect("欢迎会员.aspx");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script>alert('错误!请重新输入用户名或密码!');</script>");
}
dr.Close();
}
else if ("管理员" == DropDownList1.Text)
{
cmd.CommandText = "select * from 管理员表 where 管理员用户名 = '" + TextBox1.Text + "'and 密码='" + TextBox2.Text + "'";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["user"] = "abc";
MessageBox.Show("欢迎管理员");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script>alert('错误!请重新输入用户名或密码!');</script>");
}
dr.Close();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
cn.Close();
}
解决方案 »
- 怎么设置表单的值?
- asp.net 数据库 asp_net_User Membership role等表怎么生成的
- JScript 运行时错误: getElementById(...)' 为空或不是对象
- wcf服务 怎么才能传递Dictionary[] 参数
- 怎样用c#获取access的主键字段名?
- 请教一下关于DataList的使用方法?
- 高手请进,一个加密的sql脚本,不知道如何解码?
- Crystal Report的打印问题
- 那位大虾能告诉我用oledbconnection连接sybase数据库的连接字符串怎样写?谢谢,在线等待
- 导出xml时怎么让别人选择保存路径,类似于上传控件那样
- The server tag is not well formed.
- 关于一个简单的三层架构
你可以用Application,但是耗内存一般是登录后,把数据库中一个字段置1,然后退出登录或Session TimeOut时置回0别处登录时,如果那个字段已经是1,就不允许登录。
public class Lg
{
public string _userid;
public string _pwd;
public string _mylevel;
public string _logintime;
public string _loginip;
..........
}//login.aspx
登录验证完成
Lg g= new Lg();
g._userid=xxxxxx;
g._pwd=xxxxxx;
g._mylevel=xxxxxx;
g._logintime=xxxxxx;
g._loginip=xxxxxx;Session["login"]=u;
//页面调用
Lg g=new Lg();
g=(Session["login"] as Lg);
下面就可以调用g._userid/_pwd这类的东西,不知道行不行.我只是这样想的.对不对楼主可以参考.毕竟我写的只适合我用的程序
或者
这样,每次登陆后给值存的一个lbl里,当然了lbl是隐藏的
读lbl的值不会出错吧~ 哈哈
小偏方~
提供一种思路给大家,这也正是目前我打算用来解决这个问题的思路。
考虑利用数据锁定来实现,在程序中生成一个唯一的容器,每个用户登录之后根据用户的惟一标识生成一个唯一的KEY对象,然后把这个KEY对象放到容器中进行管理,每次新用户登录都到容器中检查,是否存在这个KEY对象,存在则拒绝,不存在则把KEY对象加到容器中!