//这是我在登录页面写的代码
int nCurUserId =-1;
//用户id
int nCurUserPower = 1;//用户权限
//从数据库中得到数据
nCurUserId = (int)myReader["id"];//
nCurUserPower = (int)myReader["UserPower"];
//写入session
Session.Add("UserName", myReader["UserName"].ToString());
Session.Add("UserId", nCurUserId);
Session.Add("UserPower", nCurUserPower);
//写cookie
HttpCookie cookie = new HttpCookie("hstebbs");
cookie.Values.Add("UserId",myReader["id"].ToString());
cookie.Values.Add("UserName",myReader["UserName"].ToString());
cookie.Values.Add("UserPower",myReader["UserPower"].ToString());
cookie.Values.Add("UserPasseord",Password.Text);
//cookie.Expires = DateTime.MaxValue;
DateTime dt = DateTime.Now;
cookie.Expires =dt.AddDays(30);
if(Request.Cookies["hstebbs"]!=null)
Response.SetCookie(cookie);
//cookie存在就更新
else
Response.AppendCookie(cookie);//不存在就添加
//进入这儿,表示已经验证通过是合法用户了
Response.Redirect("mainframeset.htm");//登录成功则进入主框架页面
//其中的一个页面这样写
if( !Page.IsPostBack )
{
if(Session["UserName"] ==null)
{
HttpCookie cookie=Request.Cookies["hstebbs"];
if(cookie!=null)
{
int nCurUserId =-1;
int nCurUserPower = 1;
//默认权限
nCurUserId = Convert.ToInt32(cookie.Values["UserId"].ToString());
nCurUserPower = Convert.ToInt32(cookie.Values["UserPower"].ToString());
string strUserName = cookie.Values["UserName"].ToString();
Session.Add("UserName", strUserName); Session.Add("UserId", nCurUserId);
Session.Add("UserPower", nCurUserPower);
}
else
{
//在win2000下只要打了补丁,就会到这里来,不知道为什么,在98,xp下则没有问题
//????????????????????????????????????????????????????????请大家帮忙
Label2.Text ="您还没有登录";
return;
}
}