当前位置: 代码迷 >> ASP.NET >> 记住用户名 !解决方案
  详细解决方案

记住用户名 !解决方案

热度:5557   发布时间:2013-02-25 00:00:00.0
记住用户名 !
有个 checkBox 如果勾选 则记录用户名 下次登陆就不要再输入咯 ! 我知道用 COOKIES 但.net中我不会搞 ! 大侠帮忙 !

------解决方案--------------------------------------------------------
探讨
JScript code<script language="javascript" type="text/javascript">function getCookies()
{var cookies= document.cookie.split(";");for(var i=0; i<cookies.length; i++)
{var cookInfo= cookies[i].split("=");if(cookInfo[0]=="userInfo") 
{var userInfo= cookInfo[1].split("#");if(document.getElementById("txtName").value== userInfo[0])
{
document.getElementById("txtPwd").value= userInfo[1];
alert(userInfo[1]);


}
}</script><form id="form1" runat="server"><div><asp:TextBox ID="txtName" runat="server"></asp:TextBox><br/><asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox><br/><asp:CheckBox ID="chkCookie" runat="server" Text="记住密码"/><asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="Login"/></div></form>

后台:按钮事件
C# codeif (chkCookie.Checked)
{
HttpCookie cookies=new HttpCookie("userInfo");
cookies.Value= txtName.Text+"#"+ txtPwd.Text;
cookies.Expires= DateTime.Now.AddDays(14);
Response.Cookies.Add(cookies);
Response.Cookies.Add(new HttpCookie("Test"));
Response.Redirect("newPage.aspx");
}


------解决方案--------------------------------------------------------
C# code
                    //-------------将用户和密码保存到cookie里                    HttpCookie bcookie = new HttpCookie("UserInfo");                    bcookie.Values.Add("User_Name",DBClass.EncryptCookie( tbUserName.Value.Replace(" ", "")));                    bcookie.Values.Add("User_Pwd", MD5(TextBox_Pwd.Text));                    bcookie.Values.Add("User_Power", DBClass.EncryptCookie("0"));                    //--------------读取要保存的时间                    bcookie.Expires = DateTime.Now.AddDays(1);                    Response.Cookies.Add(bcookie);                 HttpCookie cookies = Request.Cookies["UserInfo"];                Label1.Text = DBClass.DecryptCookie(cookies.Values["User_Name"]);
------解决方案--------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
HttpCookie aCookie = Request.Cookies["userInfo"];
if (aCookie != null)
{
Employee el = new Employee();
el.UserID = aCookie.Values["userName"];
if (el.IsOnline())//判断用户是否在线,若在线则删除若不在则直接登陆
{
el.DelOnLine(aCookie.Values["userName"]);
}
User_login(aCookie.Values["userName"], aCookie.Values["password"]);
}
else if (!string.IsNullOrEmpty(Request.QueryString["Name"]) && !string.IsNullOrEmpty(Request.QueryString["Pass"]))
{
User_login(Request.QueryString["Name"], Request.QueryString["Pass"]);
}

}

}
protected void ibtnLogOn_Click(object sender, EventArgs e)
{
if (Ch_Cookie.Checked)
{
HttpCookie cok = Request.Cookies["userInfo"];
  相关解决方案