登录时简单的记住密码和自动登录功能实现

$(document).ready(function(){

            //Login Aciton
            $("#login").bind("click", function () {

                var username = $("#username").val();
                var password = $("#password").val();

                if(username == ""){
                    alert("请输入用户名!");
                    return;
                }

                if(password == ""){
                    alert("请输入密码!");
                    return;
                }

                <%--var s = "";--%>
                <%--s += "{";--%>
                <%--s += "username:'" + username + "',password:'" + password;--%>
                <%--s += "'}";--%>
                <%--arr = eval('(' + s + ')');--%>
                <%--$.post("<%=request.getContextPath()%>/login.asp", arr, function(data){--%>
                    <%--if(data == 1){--%>
                        <%--location.replace("<%=path %>/page/index.jsp");--%>
                    <%--}else{--%>
                        <%--location.replace("<%=path %>/page/fail.jsp");--%>
                    <%--}--%>
                <%--});--%>
                $("#loginForm").submit();   //form表单提交
            });

            if ($.cookie("automaticLogin") == "true") {
                $("#automaticLogin").attr("checked", true);
                $("#rememberMe").attr("checked", true);
                $("#username").val($.cookie("usernameCookie"));
                $("#password").val($.cookie("passwordCookie"));
                $("#login").click();
            }

            if ($.cookie("rememberMe") == "true") {
                $("#rememberMe").attr("checked", true);
                $("#username").val($.cookie("usernameCookie"));
                $("#password").val($.cookie("passwordCookie"));
            }

            //Automatic Login Checkbox
            $("#automaticLogin").bind("click", function () {
                if($(this).is(":checked") == true){
                    // 存储一个带7天期限的 cookie
                    $.cookie("automaticLogin", "true", { expires: 7 });
                    $.cookie("rememberMe", "true", { expires: 7 });
                    $.cookie("usernameCookie", $("#username").val(),{expires:7});
                    $.cookie("passwordCookie", $("#password").val(),{expires:7});
                }else{
                    $.cookie("automaticLogin", "false", { expires: -1 });
                }
            });

            //Remember Password Checkbox
            $("#rememberMe").bind("click", function () {
                if($(this).is(":checked") == true){
                    // 存储一个带7天期限的 cookie
                    $.cookie("rememberMe", "true", { expires: 7 });
                    $.cookie("usernameCookie", $("#username").val(),{expires:7});
                    $.cookie("passwordCookie", $("#password").val(),{expires:7});
                }else{
                    $.cookie("automaticLogin", "false", { expires: -1 });
                    $.cookie("rememberMe", "false", { expires: -1 });
                    $.cookie("usernameCookie", '', { expires: -1 });
                    $.cookie("passwordCookie", '', { expires: -1 });
                }
            });
        });


因为用到cookie所以要导入jquery.cookie.js


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 WinForm 中实现记住密码自动登录功能,需要将用户输入的账号和密码保存到本地,下次启动程序自动读取已保存的信息进行登录。以下是一个简单实现步骤: 1. 在登录页面添加“记住密码”和“自动登录”选项,并将用户输入的账号和密码保存到本地,可以使用配置文件、注册表、数据库等方式进行存储。 2. 在程序启动检查本地是否保存了用户登录信息,如果有则自动填充账号和密码,并触发登录按钮的 Click 事件进行自动登录。 3. 在用户手动退出登录,清除本地保存的登录信息。 下面是一个示例代码: ```csharp private void LoginForm_Load(object sender, EventArgs e) { // 读取保存的登录信息 if (Properties.Settings.Default.RememberMe) { txtUsername.Text = Properties.Settings.Default.Username; txtPassword.Text = Properties.Settings.Default.Password; chkRememberMe.Checked = true; } if (Properties.Settings.Default.AutoLogin) { btnLogin.PerformClick(); } } private void btnLogin_Click(object sender, EventArgs e) { // 登录验证 if (ValidateUser(txtUsername.Text, txtPassword.Text)) { // 保存登录信息 if (chkRememberMe.Checked) { Properties.Settings.Default.Username = txtUsername.Text; Properties.Settings.Default.Password = txtPassword.Text; Properties.Settings.Default.RememberMe = true; Properties.Settings.Default.Save(); } else { Properties.Settings.Default.Username = ""; Properties.Settings.Default.Password = ""; Properties.Settings.Default.RememberMe = false; Properties.Settings.Default.Save(); } if (chkAutoLogin.Checked) { Properties.Settings.Default.AutoLogin = true; Properties.Settings.Default.Save(); } // 登录成功 this.DialogResult = DialogResult.OK; } else { // 登录失败 MessageBox.Show("登录失败,请检查账号和密码是否正确。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnLogout_Click(object sender, EventArgs e) { // 清除登录信息 Properties.Settings.Default.Username = ""; Properties.Settings.Default.Password = ""; Properties.Settings.Default.RememberMe = false; Properties.Settings.Default.AutoLogin = false; Properties.Settings.Default.Save(); // 退出登录 this.DialogResult = DialogResult.Cancel; } ``` 其中,Properties.Settings.Default 是一个应用程序配置文件,用于保存应用程序的配置信息。ValidateUser() 方法用于验证用户输入的账号和密码是否正确。需要注意的是,在实现自动登录功能,需要先实现记住密码功能,否则自动登录无法读取到密码信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值