登录时记住密码

来源:互联网 发布:企业宣传制作软件 编辑:程序博客网 时间:2024/04/30 10:42

//实现登录;
            string loginId = this.TextBox1.Text;
            string loginPwd = this.TextBox2.Text;
            BLL.UserManager um = new BLL.UserManager();
            Model.User currentUser;
            bool isSuccess = um.Login(loginId, loginPwd, out currentUser);
            if (isSuccess) //登录成功;
            {
                //处理记住密码-- 必须通过cookie记下来;页面类的User属性才能取得当前用户的信息
                if (this.CheckBox1.Checked)
                {
                    string ddlValue = this.ddlTime.SelectedValue;
                    DateTime outTime = DateTime.Now;
                    if(ddlValue=="1年")                   
                        outTime = DateTime.Now.AddYears(1);
                     else if(ddlValue=="1个月")
                        outTime = DateTime.Now.AddMonths(1);
                    else if(ddlValue=="1周")
                        outTime = DateTime.Now.AddDays(7);


                    //MS设计一个票据类来封装用户信息;
                    //1、生成票据;票据等相关安全处理的类:System.Web.Security
                    FormsAuthenticationTicket ticket =
                        new FormsAuthenticationTicket(1, currentUser.LoginId, DateTime.Now, outTime, true, currentUser.Name, FormsAuthentication.FormsCookiePath);
                    //2、对票据进行加密;
                    string enctrytData = FormsAuthentication.Encrypt(ticket);
                    //3、将加密后的数据写到cookie中;
                    HttpCookie cookie = new HttpCookie("userInfo");
                    cookie.Value = enctrytData;
                    cookie.Expires = outTime;
                    Response.Cookies.Add(cookie);
                    //4跳转到对应的某一页面;

                    FormsAuthentication.RedirectFromLoginPage(currentUser.Name, true);
                    //Response.Redirect(FormsAuthentication.GetRedirectUrl(currentUser.LoginId, true));
                    //Response.Redirect("Cart.aspx");
                }
            }
            else
            {
                this.litMessage.ForeColor = Color.Red;
                this.litMessage.Text = "请检查帐号与密码";
            }

原创粉丝点击