且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何记住用户名和密码使用记住我的cookie

更新时间:2022-10-23 17:47:26

试试这个:



下次在asp.net的登录控制中记住我 [ ^ ]



http://www.c-sharpcorner.com/uploadfile/raj1979/login-control-in-Asp-Net-3-5/ [ ^ ]


嗨亲爱的,如果你使用TextBox控件作为用户名和密码,你可以按照这个。





 受保护  void  btn_Click(  object  sender,EventArgs e)
{
// GetValues();
HttpCookie cookie = new HttpCookie( UserName);
cookie.Values.Add( this .userName.Text, this .password.Text) ;
Response.Cookies.Add(cookie);
}





这里有一些javascript代码



< pre>函数readTheCookie(){
var the_cookie = document 跨度> .cookie;
if (the_cookie!= ){
document .getElementById(' <%= password.ClientID%>')。value = the_cookie.split(' =')[ 2 ];
}
}





[已删除乞讨投票]



谢谢

好​​运


Hi, everyone
i Need solution for the following concept in ASP.Net
When the user types username and password with the "Remember ME" checkbox checked it will stored the username and password in the cookie inside the system.
Whenever the user logins again, when username is entered in the textbox the password must be automatically loaded from the cookie. simultaneously the cookie must store multiple username and passwords.

Here is the code for
CS.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<script type="text/javascript" language="javascript">

//function writeCookie()
//{
//var txtuname= document.getElementById('txtUserName');
//var txtpass= document.getElementById('txtPassword');


//var exdate=new Date();
//exdate.setDate(exdate.getDate()+15);
//
//if(document.getElementById('chkRememberMe').checked)
//{
//alert( txtuname.value+"="+escape(txtuname.value+"|"+txtpass.value)+";expires="+exdate.toGMTString());
//document.cookie= txtuname.value+"="+escape(txtuname.value+"|"+txtpass.value)+";expires="+exdate.toGMTString();

//}
//else
//{
//alert("Check box is not selected ");

//}
//}

function readCookie()
{

var txtuname= document.getElementById('txtUserName');
var txtpass= document.getElementById('txtPassword');

debugger

if (document.cookie.length>0)
{
var c_start=document.cookie.indexOf(txtuname.value);
//alert(document.cookie);
//alert("to search txtvalue "+txtuname.value);

if (c_start!=-1)
{
c_start=c_start + txtuname.value.length +1 ;
// alert(" start "+c_start);
var c_end=document.cookie.indexOf(";",c_start);

// alert("end "+c_end);
if (c_end==-1)
{
c_end=document.cookie.length;
// alert(c_end);
}
var value= unescape(document.cookie.substring(c_start,c_end));
// alert("value "+value);
var arr = new Array(2);
arr= value.split('|',2)
alert("Username "+arr[0]);
alert("Password "+arr[1]);
txtuname.value=arr[0];
txtpass.value=arr[1];
}
else
{
txtpass.value="";
}
}

}

</script>
<body>
    <form id="form1" runat="server">
    UserName:
    <asp:TextBox ID="txtUserName" runat="server" ></asp:TextBox><br />
    Password:&nbsp;
    <asp:TextBox ID="txtPassword" TextMode="Password" AutoCompleteType="None" AutoPostBack="false" runat="server"></asp:TextBox><br />
    Remember me:
    <asp:CheckBox ID="chkRememberMe" runat="server" /><br />
    <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="Login_Click" />
        <asp:Button ID="Button1" runat="server"  Text="Delete" />
    </form>
</body>
</html>




Here is the code for CS.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null)
            {
                txtUserName.Text = Request.Cookies["UserName"].Value;
                //txtPassword.Attributes["value"] = Request.Cookies["Password"].Value;
                //txtPassword.Text = Request.Cookies["Password"].Value;
                txtPassword.Attributes.Add("OnFocus", "javascript:readCookie();");


            }

    }

    protected void Login_Click(object sender, EventArgs e)
    {
        //btnLogin.Attributes.Add("OnClick", "javascript:writeCookie();");
            if (chkRememberMe.Checked)
            {
                Response.Cookies["UserName"].Value = txtUserName.Text;
                Response.Cookies["Password"].Value = txtPassword.Text;
                Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(30);
                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(30);
                //Response.Cookies.Add("");
                Response.Redirect("Default.aspx");
            }
            else
            {
                Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
                Response.Redirect("Default.aspx");

            }
            Response.Cookies["UserName"].Value = txtUserName.Text.Trim();
            Response.Cookies["Password"].Value = txtPassword.Text.Trim();

        }
        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null)
        //    {
        //        Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-30);
        //        Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-30);
        //    }
        //    txtUserName.Text = "";
        //    txtPassword.Text = "";
        //}

    //protected void txtUserName_TextChanged(object sender, EventArgs e)
    //{
    //    txtPassword.Attributes.Add("OnTextChanged", "javascript:readCookie();");
    //}
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}










Thanks in advance....

Try this:

Remember me next time in login control in asp.net[^]

http://www.c-sharpcorner.com/uploadfile/raj1979/login-control-in-Asp-Net-3-5/[^]


Hi Dear if you are using TextBox controls for username and password you can follow this.


protected void btn_Click(object sender, EventArgs e)
        {
            //GetValues();
            HttpCookie cookie = new HttpCookie("UserName");
            cookie.Values.Add(this.userName.Text, this.password.Text);
            Response.Cookies.Add(cookie);
        }



here is some javascript code

<pre>function readTheCookie() {
            var the_cookie = document.cookie;
            if (the_cookie != "") {
                document.getElementById('<%=password.ClientID %>').value = the_cookie.split('=')[2];
            }
        }



[Removed begging for vote]

Thanks
Good Luck