网页登陆 记住密码

页面登陆记住密码,用的是浏览器cookie保存用户名和密码

关键代码:

            if (this.CheckBox1.Checked)
            {
                    Response.Cookies["userAdmin"]["userName"] = this.userid.Text.Trim();  //保存用户名
                    Response.Cookies["userAdmin"]["pwd"] = this.pwd.Text.Trim();                  //保存密码
                    Response.Cookies["userAdmin"].Expires = DateTime.Now.AddDays(1); //失效时间
            }

 

重新登陆的时候,在page_load里面加代码:

                if (Request.Cookies["userAdmin"] != null)
                {
                    HttpCookie aCookie = Request.Cookies["userAdmin"];
                    userName = aCookie.Values["userName"];
                    pwd1 = aCookie.Values["pwd"];

 

                   this.userid.Text = userName;
                   this.pwd.Attributes.Add("value",pwd1);

                }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
记住密码功能可以通过在用户登录时,将用户的账号和密码保存到 Cookie 中实现。当用户下次访问网站时,检查是否存在 Cookie,如果存在则自动填充账号和密码。 具体实现步骤如下: 1. 在用户登录时,如果用户选择了记住密码,则将用户的账号和密码保存到 Cookie 中。 ```java if (rememberMe) { // 创建 Cookie 对象 Cookie usernameCookie = new Cookie("username", URLEncoder.encode(username, "UTF-8")); Cookie passwordCookie = new Cookie("password", URLEncoder.encode(password, "UTF-8")); // 设置 Cookie 的有效期,单位为秒 usernameCookie.setMaxAge(7 * 24 * 60 * 60); passwordCookie.setMaxAge(7 * 24 * 60 * 60); // 将 Cookie 添加到响应中 response.addCookie(usernameCookie); response.addCookie(passwordCookie); } ``` 2. 在网页加载时,检查是否存在 Cookie,并将账号和密码填充到登录表单中。 ```javascript // 检查是否存在 Cookie var usernameCookie = getCookie("username"); var passwordCookie = getCookie("password"); if (usernameCookie != "" && passwordCookie != "") { // 填充账号和密码 $("#username").val(decodeURIComponent(usernameCookie)); $("#password").val(decodeURIComponent(passwordCookie)); $("#rememberMe").prop("checked", true); } ``` 3. 在用户退出登录时,删除保存在 Cookie 中的账号和密码。 ```java // 删除 Cookie Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("username") || cookie.getName().equals("password")) { cookie.setMaxAge(0); response.addCookie(cookie); } } } ``` 需要注意的是,保存在 Cookie 中的账号和密码可能会被黑客窃取,因此需要对密码进行加密处理,并使用 HTTPS 协议进行传输,以保证安全。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值