1.   csdn的登陆,如果验证码填错,会弹出个alert,然后点了确定之后,除了账号还在,其他全部清空了。
      这个账号还在的效果是怎么搞的?2.   记住用户名,下次打开该网站的时候不用输入用户名了。(同理,记住密码)3.   2周内不用登陆。以上3点,很想知道怎么做的,能力有限想不出来。希望高人指点一二,谢过

解决方案 »

  1.   

    1.  ajax判断2. cookie3. 设置cookie有效期为2周
      

  2.   

    记住密码,那cookie里的密码一定要加密
      

  3.   


    正确不过还有,就是csdn也用了session,来看用户当前是否在线,
      

  4.   

    给你部分代码参考一下
     <form id="form1" runat="server">
        <asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
       <div id="Div1" class="login">
    <div class="lgnContent">
    <div class="lgnList">用户名:
      <asp:TextBox ID="UName"  runat="server" CssClass="inputbtn" MaxLength="20" Width="120px"></asp:TextBox>
      
    </div>
    <div class="lgnList">密 码:
      <asp:TextBox ID="UPsw" runat="server" TextMode="password" CssClass="inputbtn" MaxLength="20" Width="120px"></asp:TextBox>
    </div>
    <div class="lgnList">验证码:
      <asp:TextBox ID="ValidCode" runat="server"  CssClass="inputbtn" MaxLength="5" Width="40px"></asp:TextBox>
                    <a href="javascript:" onfocus="this.blur()"  onclick="document.getElementById('<%=imgbtnCode.ClientID %>').src='<%=this.ResolveClientUrl("~/AdInterface/CheckCode.aspx") %>?date='+new Date();return false;">
                    <asp:ImageButton ID="imgbtnCode" AlternateText="验证码" ToolTip="看不清,换一张"  runat="server" Enabled="false" ImageUrl="~/AdInterface/CheckCode.aspx" align="absmiddle" /> 
                    </a>
        </div>
    <div class="lgnList_btn">
    <asp:UpdatePanel runat="server" ID="UpdatePanel1">
        <ContentTemplate>
            <asp:Button runat="server" ID="BtnLogin" CssClass="lgnbtn" OnClick="BtnLogin_Click" />
            <asp:Button runat="server" ID="BtnReset" CssClass="calbtn"   />
            <asp:Label runat="server" ID="result" ForeColor="red"></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>

      
    </div>
    </div>
    </div>
    const string STR_COOKIE_USERID="AdInterface_LoginID";
    const string STR_COOKIE_USERPSW="AdInterface_LoginID";
     protected void Page_Load(object sender, EventArgs e)
        {
            UName.Attributes.Add("onfocus", "document.getElementById('" + result.ClientID + "').innerText=''");
            UPsw.Attributes.Add("onfocus", "document.getElementById('" + result.ClientID + "').innerText=''");
            if (!IsPostBack)
            {
               
                if (Request.Cookies["AdInterface_LoginID"] != null) //调入保存帐号的cookie
                {
                    this.UName.Text = Request.Cookies[STR_COOKIE_USERID].Value.ToString();
                    this.UPsw.Text = CommUtility.Descryp(Request.Cookies[STR_COOKIE_USERPSW].Value.ToString());
                }
            }    }
    protected void BtnLogin_Click(object sender, EventArgs e)
        {
            string id, psw;
            id = CommUtility.Regular(this.UName.Text.Trim());
            psw = CommUtility.Regular(this.UPsw.Text);        if (Session["CHECKCODE"].ToString().ToLower() != ValidCode.Text.ToLower())
            {
                result.Text = "验证码错误!";
                return ;
            }
            ServiceInterface service = new ServiceInterface();
            if (service.CheckUserLogin(id, psw)) //合法
            {
                HttpCookie cookieID = new HttpCookie(STR_COOKIE_USERID);
                HttpCookie cookiePsw = new HttpCookie(STR_COOKIE_USERPSW);
                cookieID.Expires = DateTime.Now.AddDays(14);//帐号cookie保存时间为2周
                  cookiePsw.Expires = .Now.AddDays(14);
                cookieID.Value = id;
                cookiePsw.Value = CommUtility.EncrypDateTime(psw);
                Response.Cookies.Add(cookieID);
                Response.Cookies.Add(cookiePsw);
                string scriptStr = "top.location.replace('OrderReports/AdMediaSharePage.aspx');";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", scriptStr, true); 
                
            }
            else
            {
                result.Text = "用户名或密码错误!";
                return;
            }
           
        }
      

  5.   

    改成const string STR_COOKIE_USERPSW="AdInterface_LoginPSW";
      

  6.   

    1:为了方便,验证码放到后台判断.
    2,3:cookie
      

  7.   


    我们做个CSDN 的论坛的部分功能  发部分代码给你看下  希望对你有所帮助代码如下 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string name = this.TextBox1.Text.Trim().ToString();
            string pass = this.TextBox2.Text.Trim().ToString();
            if(name != "" && pass != "")
            {            if (new UsersBll().ExistsLoginNames(name, pass))
                {
                    string number = this.TextBox3.Text;
                    if (!Rnum.CheckSN(number))  //asp.net第三方控件 生成的验证码 此方法用于检查输出的是否和验证码一致
                    {
                      
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "s", "alert('验证码错误')", true);//如果不一致弹出提示框
                    }
                    else
                    {
                        if (CheckBox1.Checked == true) 如果选中的话 则把它放入COOKIE中
                        {
                            HttpCookie cook = new HttpCookie("MyCook");  
                            cook.Values["name"] = name;
                            cook.Expires = DateTime.Now.AddDays(14);
                            Response.Cookies.Add(cook);
                        }
                        Session["name"] = name;
                        
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "s", "parent.window.location.href='Index.aspx'", true);
                    }
                }
                else
                {
                  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "s", "alert('你提交的信息不正确')", true);
                }
            }
            else
            {
                
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "s", "alert('你的信息提交不完整,请核对后在提交')", true);
            }
      

  8.   

    ----------------------------------------------------------------------
    给你个思路吧:
      1. 一般验证码都是保存在Sesseion中,在输入验证码的文件框失去焦点时,通过Ajax判断输入的验证码和Session中的验证码是否一致,根据AJAX返回信息进行判断.
      2. 用户登陆后可以写一个Cookie来的保存用户信息,在页面Onload时读取Cookie中的用户信息,若不为空就赋给控件
      3. 2周内不用登陆用的是Cookie保存用户信息.
      

  9.   

    Cookie的Expires设置为两周的时间