如何记住用户名和密码使用记住我的cookie [英] How to Remember the username and password using remember me in cookies

查看:85
本文介绍了如何记住用户名和密码使用记住我的cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

i需要ASP.Net中以下概念的解决方案

当用户输入用户名和密码并选中记住我复选框时,它将被存储系统内cookie中的用户名和密码。

每当用户再次登录时,在文本框中输入用户名时,必须从cookie自动加载密码。同时cookie必须存储多个用户名和密码。



这是代码

CS.aspx

 <%@       语言  =  C#    AutoEventWireup   =  true    CodeFile   =  CS.aspx.cs   继承  =  _默认   %&g t;  

< !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 = 服务器 >
< title > < / title >
< / head >
< script 类型 = 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);
//
// 如果(document.getEle mentById('chkRememberMe')。已选中)
// {
// alert(txtuname.value +=+ escape(txtuname.value +|+ txtpass.value) +; expires =+ exdate.toGMTString());
// 文档。 cookie = txtuname.value +=+ escape(txtuname.value +|+ txtpass.value)+; expires =+ exdate.toGMTString();

// }
// else
// {
// alert(未选中复选框);

// }
// }

function readCookie()
{

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

调试器

如果 document .cookie.length> 0)
{
var c_start = 文档 .cookie.indexOf(txtuname.value);
// alert(document.cookie);
// alert(搜索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( < span class =code-string>;
,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 数组 2 );
arr = value.split(' |' 2
alert( 用户名 + arr [ 0 ]);
alert( 密码 + arr [ 1 ]);
txtuname.value = arr [ 0 ];
txtpass.value = arr [ 1 ];
}
else
{
txtpass.value = ;
}
}

}

< / 脚本 >
< 正文 >
< 表格 id = form1 runat = server >
用户名:
< asp :TextBox ID = txtUserName runat = server > < / asp:TextBox > < br / >
密码:& nbsp;
< asp:TextBox ID = txtPassword TextMode = 密码 AutoCompleteType = AutoPostBack < span class =code-keyword> =
false runat = server > < / asp:TextBox > < br / >
请记住我:
< asp:CheckBox ID = chkRememberMe runat = server / > < br / >
<
asp:按钮 ID = btnLogin runat = server 文字 = < span class =code-keyword>登录 OnClick = Login_Click / >
< asp:按钮 ID = Button1 runat = server 文字 = 删除 / >
< / form >
< / body >
< / html >







这里是CS.aspx.cs的代码



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

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


}

}

受保护 void Login_Click( object sender,EventArgs e)
{
// btnLogin.Attributes.Add(OnClick,javascript:writeCookie(););
if (chkRememberMe.Checked)
{
Response.Cookies [ UserName]。Value = txtUserName.Text;
Response.Cookies [ 密码]。Value = txtPassword.Text;
Response.Cookies [ UserName]。Expires = DateTime.Now.AddDays(< span class =code-digit> 30
);
Response.Cookies [ 密码]。Expires = DateTime.Now.AddDays(< span class =code-digit> 30 );
// Response.Cookies.Add();
响应。重定向( Default.aspx);
}
else
{
Response.Cookies [ UserName]。Expires = DateTime.Now.AddDays(-1);
Response.Cookies [ 密码]。Expires = DateTime.Now.AddDays( - 1);
Response.Redirect( Default.aspx);

}
Response.Cookies [ UserName]。 Value = txtUserName.Text.Trim();
Response.Cookies [ 密码]。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);
// }
// txtUser Name.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)
{

}
}



< br $>














提前致谢....

解决方案

试试这个:



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



http://www.c-sharpcorner.com/uploadfile/raj1979/login-control-in-Asp-Net-3-5/ [<一个href =http://www.c-sharpcorner.com/uploadfile/raj1979/login-control-in-Asp-Net-3-5/target =_ blanktitle =New Window> ^ ]


嗨亲爱的,如果你使用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 lang =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


这篇关于如何记住用户名和密码使用记住我的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆