namespace Web.Controls.ConLogin
{
    public partial class UserLogin1 : System.Web.UI.UserControl
    { //建立cookie
        System.Web.HttpCookie newcookie = new HttpCookie("user");
        Model.User myUser = new Model.User();
        BLL.UserBLL myUserBLL = new UserBLL();
        
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {   //判断Cookies是否存在
                if (Response.Cookies["user"]["userName2"]!= null)
                {//判断用户名存在
                    if (myUserBLL.CheckUserName(Response.Cookies["userName2"].Value.ToString()))
                    Response.Redirect("~/Test2.aspx");
                }
            }
        }
        protected void LinkLogin1_Click(object sender, EventArgs e)
        {
          
            //验证码
            if (txtPic1.Text == Request.Cookies["CheckCode"].Value)
            {                //用户名和密码验证. 
                string uid;
                string pwd;                uid = Server.HtmlDecode(txtName1.Text);                pwd = Server.HtmlDecode(txtPass1.Text);
                myUser.userName = uid;
                myUser.userPassword = pwd;
                if (myUserBLL.BLLCheckUser (uid ,pwd ))
                {
             
                    //用户是否选择记住cookie
                    if (remberCookies.Checked == true)
                    {                        if ( Response.Cookies["userName2"].Value == null)
                        {
           
                            newcookie.Values["userName2"] = txtName1.Text.Trim();
                            newcookie.Values["userPass2"] = txtPass1.Text.Trim();
                            newcookie.Expires = DateTime.Now.AddDays(1);
                            Response.Cookies.Add(newcookie);                            //Response.Cookies["userName1"].Value = txtName1.Text.Trim();
                            //Response.Cookies["userPass1"].Value = txtPass1.Text.Trim();
                            //Response.Cookies["userName1"].Expires = DateTime.Now.AddDays(1);
                            //Response.Cookies["userPass1"].Expires = DateTime.Now.AddDays(1);
                             }}   
    }
}
       

解决方案 »

  1.   

    要设置过期时间的。否则你一关网站就过期
    cookie实例名.Empires=DateTime.Now.AddMonth(3);
      

  2.   

    哦设置了啊。
         if ( Response.Cookies["userName2"].Value == null) 
                            { 
              
                                newcookie.Values["userName2"] = txtName1.Text.Trim(); 
                                newcookie.Values["userPass2"] = txtPass1.Text.Trim(); 
                                newcookie.Expires = DateTime.Now.AddDays(1); 
                                Response.Cookies.Add(newcookie);
    估计是if ( Response.Cookies["userName2"].Value == null) 的问题
      

  3.   

    if ( Response.Cookies["userName2"].Value == null) 都等于null了,你让它有效期加一天,还不是空的呀,
      

  4.   

    if ( Response.Cookies["userName2"].Value == null) 都等于null了,就是说,没有cookie然后建一个cookie这儿没有错多嘛.
      

  5.   

       if ( Response.Cookies["userName2"].Value == null) 
                            { 
                                    //得到用户输入的用户名
                                newcookie.Values["userName2"] = txtName1.Text.Trim(); 
                          //得到用户输入的密码
                                newcookie.Values["userPass2"] = txtPass1.Text.Trim(); 
      

  6.   

    Response.Cookies.Add(newcookie); 
    这里你可以尝试修改为:
    Response.AppendCookie(newcookie);